Today I tried to access coursera api using jquery after reading Coursera documentation . I wrote the code and received an error message. No 'Access-Control-Allow-Origin' header is present on the requested resource.So I did some google and found that Jsonp can be used to request a cross-domain. So I just used the $ .ajax function to query this url or say it is a simple URL and some other similar URLs, but failed.
URL data is similar to {"elements":[{"id":2,"shortName":"ml","name":"Machine Learning","links":{}}],"linked":{}}
I wrote the following code.
$(document).ready(function() {
$.ajax({
url: "https://api.coursera.org/api/catalog.v1/courses/2",
type: "GET",
dataType: 'jsonp',
jsonpCallback: 'localJsonpCallback',
contentType: 'application/json',
success: function(){
alert("success");
},
error:function(jqxhr, textStatus, error){
alert("textStatus : " + textStatus + "\n error" + error);
}
});
function localJsonpCallback(data) {
alert("localJsonpCallback : " + data);
}
});
, - textstatus: parseError Error: localJsonpCallback was not called. , . , Uncaught SyntaxError: Unexpected token : 2?callback=localJsonpCallback&_=1418037208234:1 url https://api.coursera.org/api/catalog.v1/courses/2.
jsonp? .