Reputation: 33
I'm relatively new to jquery/ajax, and I'm having trouble parsing the results from a very simple JSON ajax call. Here's my code. There are 2 test URLs in this example: the commented URL works as expected, but the uncommented URL does not (they are both properly formatted JSON, according to jslint). Any idea why one would throw a parse error, and the other would not (they are both 3rd party domains)?
Thank you in advance!
function getNews2() {
$.ajax({
//url: "http://api.ihackernews.com/page?format=jsonp",
url: "http://recs.coremetrics.com/iorequest/restapi?cm_cid=90232642&cm_zoneid=Mobile&cm_targetid=FULO-0101",
dataType: "jsonp",
success: function(data, textStatus, xhr) {
alert("SUCCESS recsStatus=" + textStatus);
alert(JSON.stringify(data));
},
error: function(data, textStatus, errorThrown) {
alert("FAILURE recsStatus=" + textStatus);
alert(JSON.stringify(data));
}
});
}
getNews2();
Upvotes: 3
Views: 2710
Reputation: 120168
after looking at responses from both URLs with CURL, the one that doesn't work is not a jsonp response. It's a json response. It's not wrapped in the method call required by json p to work.
The one that doesn't work is
{"io":{"rec_count":12,...}}
vs
HNCallback({"nextId":null,...})
see the difference?
You won't be able to get a pure json response from either URL because of the same origin policy. So you need to use CORS (if you control the other domains), or jsonp, like you are currently configured to use, or server side proxy.
Upvotes: 2