I am trying to write a HTML5 mobile application and use jQuery to get a json from the url http://cin.ufpe.br/~rvcam/favours.json I tried using
var url='http://cin.ufpe.br/~rvcam/favours.json';
$.getJSON(url, function(data, status)
{
console.log(data);
console.log(status);
});
but nothing shows up on the console. I don't see what I am doing wrong.
[EDIT] I learned from another post that I can't normally retrieve information from another server. But this server in particular (cin.ufpe.br/~rvcam) is mine. Can I use PHP or some other method to allow my application to retrieve the data?
2 Answers 2
The URL doesn't return valid json. It returns some JavaScript that attempts to execute a function called "foo" and passes the object as an argument. This is commonly called "jsonp". It is a method of achieving cross domain ajax calls
2 Comments
Your http://cin.ufpe.br/~rvcam/favours.json file isn't valid json. The valid json is wrapped in foo(). Remove the foo() from that file and it will work.
.fail()callback and see if it's raising an error. The arguments will match$.ajax()'serroroption --(xhr, status, error).foo()will need to be removed from the file so that it's just JSON and the server will need to respond with CORS headers that allow the request.$.ajax(url, {dataType:"jsonp",jsonpCallback:"foo"}).done(function(data) {...});will tell jQuery to expect jsonp with the callback namefoo.