Reputation: 59
I am trying to get JSON file with JSONP:
function getJson() {
var url = 'http://www.oref.org.il/WarningMessages/alerts.json?callback=?';
$.getJSON(url, function(data) {
checkJson(data);
});
setTimeout(arguments.callee, 2000);
}
getJson();
The problem is that I am getting this error:
Uncaught SyntaxError: Unexpected token :
So I searched online and I found few solutions, but non of them worked.
I tried to use Ajax instead and I tried to replace $.getJSON
with $.parseJSON
but I got this error:
Uncaught SyntaxError: Unexpected token h
The JSON file:
{
"id" : "1406032249536",
"title" : "test",
"data" : []
}
Upvotes: 2
Views: 4130
Reputation: 943561
Because you have a callback=?
in your URL, you are making a JSONP request.
The response for a JSONP request must be a JavaScript application. It should consist of a function call (to the function defined by (in PHP terms) $_GET['callback']
) with one argument (the data you are asking for).
You are getting JSON back, so the service you are trying to connect to does not support JSONP (at least not with the standard callback
argument — consulting their documentation might give you a different argument to use).
You could make a standard XMLHttpRequest (by taking the callback=?
out), but you said (in comments) that that gives you a complaint about Access-Control-Allow-Origin
not giving your site permission to access it.
If you control the JSON endpoint, you could change it so it supports either JSONP or CORS (which is what provides Access-Control-Allow-Origin
). It sounds like you do not control that endpoint though, so that isn't an option.
This leaves you with one option: Use a proxy.
Make the Ajax request to your own server. Have a server side program running on your server make the request to the JSON endpoint and return the data.
You could also look at using a third party proxy, such as YQL, but that will probably suffer from the geographic restrictions you mentioned in another comment on the question.
Upvotes: 2