1

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...

Phil
  • 157,677
  • 23
  • 242
  • 245
Sam.E
  • 175
  • 2
  • 10
  • Does Coursera even offer a JSONP option? If not, why would you even bother trying it? – Phil Dec 06 '15 at 23:50

1 Answers1

2

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'
    }
})
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Thank you, this worked. Could use explain what the "u" parameter does? – Sam.E Dec 07 '15 at 01:04
  • 1
    @Sam.E That's the `u` query parameter in `http://cors.io/?u=...`. URL parameters need to be [encoded](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) which is what Angular does to the `params` object when constructing the request. – Phil Dec 07 '15 at 01:07