Access Coursera api data form using jquery jsonp

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

+4
3

success

$(document).ready(function() {
  $.ajax({
    url: "https://api.coursera.org/api/catalog.v1/courses/2",
    type: 'GET',
    dataType: "json",
    success: function(data) {
        console.log(JSON.stringify(data,null,4));
    }
  }); 
});

 {
    "elements": [
        {
            "id": 2,
            "shortName": "ml",
            "name": "Machine Learning",
            "links": {}
        }
    ],
    "linked": {}
}

,

+3

: -

$(document).ready(function() {    
$.getJSON("https://api.coursera.org/api/catalog.v1/courses?ids=2,3&fields=language,shortDescription&includes=sessions&fields=status&categories", function (response) {
alert(JSON.stringify(response));
},'jsonp'); 
 });

+2

jsonp, ajax

$.ajax({
    url: "https://api.coursera.org/api/catalog.v1/courses/2",
    type: "GET",
    dataType: 'json',
    success: function (res) {
      console.log(res);
    },
    error: function(jqxhr, textStatus, error) {
        alert("textStatus : " + textStatus + "\n error" + error);
    }
});

: http://jsbin.com/payare/1/edit?js,console

+2

All Articles