ajaxrestcorsangular-httpcoursera-api

AngularJS $http request to Coursera's REST API. Cross-origin denied although public key is not required


Coursera API documentation: https://tech.coursera.org/app-platform/catalog/

I tried to make a simple GET call to the api:

Like This:

$scope.courseraSearch = function(query){


    var courseraAPIUrl = 'https://api.coursera.org/api/courses.v1?q=search&query=Machine+Learning';

    $http({
      method: 'GET',
      url: courseraAPIUrl
    }).then(function successCallback(response) {
      console.log(response);
      for(i in response.data.elements){
        $scope.courseraResults.push(response.data.elements[i]);
      }
    }, function errorCallback(response) {
    });
  }

but I always get CORS error or "Refused to execute script from '*' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled." error.

I tried using cors.io as proxy:

$scope.courseraSearch = function(query){

    //https://api.coursera.org/api/courses.v1?q=search&query=Calculus
    var courseraAPIUrl = 'http://cors.io/?u=https://api.coursera.org/api/courses.v1?q=search&query=Machine+Learning';

    $http({
      method: 'GET',
      url: courseraAPIUrl
    }).then(function successCallback(response) {
      console.log(response);
      for(i in response.data.elements){
        $scope.courseraResults.push(response.data.elements[i]);
      }
    }, function errorCallback(response) {
    });
  }

But doing so, I can't seem to pass in any parameters (when I pass in "Machine Learning" query, it returns normal query as if I didn't put any search term in)

I've already tried jsonp as well...


Solution

  • Using cors.io, you should probably make sure the parameters are encoded correctly. Easiest way to do this is use the params HTTP config property

    $http.get('http://cors.io/', {
        params: {
            u: 'https://api.coursera.org/api/courses.v1?q=search&query=Machine+Learning'
        }
    })