I am making a cross domain JSONP call using this code:
jQuery.ajax({ async: true, url: 'http://mnews.hostoi.com/test.json', dataType: 'jsonp', method: "GET", error: function (jqXHR, textStatus, errorThrown) { console.log(textStatus + ': ' + errorThrown); }, success: function (data, textStatus, jqXHR) { if (data.Error || data.Response) { exists = 0; } } });
When debugging in Firebug, I get the following error:
SyntaxError: missing ; before statement
However, when I pass my json object (available through the link in the JQ code) through a tool like jsonlint.com, it says it is valid JSON. And I don't find any anomalies either. How could it be returning a syntax error? Is it some JSONP detail I am not getting or what?
{"news":[ { "sentences": [ "Neuroscientists have discovered abnormal neural activity...", "The researchers found that these mice showed many symptoms...", "\"Therefore,\" the study authors say, \"our findings provide a novel.." ], "summaryId": "ZJEmY5", "title": "Abnormal neural activity linked to schizophrenia" }]}
Thanks in advance.
JSONP is not JSON. A JSONP response would consist of a JavaScript script containing only a function call (to a pre-defined function) with one argument (which is a JavaScript object literal conforming to JSON syntax).
The response you are getting is JSON, not JSONP so your efforts to handle it as JSONP fail.
Change dataType: 'jsonp'
to dataType: 'json'
(or remove the line entirely, the server issues the correct content-type so you don't need to override it).
Since your script is running on a different origin to the JSON then you will also need to take steps (most, but not all, of which require that you control the host serving the JSON) to work around the same origin policy.
The error is because it is returning JSON not JSONP.
JSONP is supposed to look like
someCallBackString({ The Object });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With