I have 2 examples of a function that retrieves json-data and gives an alert.
In this example, everything goes fine: http://jsbin.com/uwupa3/edit
$(document).ready(function(){
var timeService = "http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?";
$.getJSON(timeService, function(data) {
alert(data);
});
});
But in the second example, there is no alert displayed.
Why? The only difference is the service where the json is retrieved. The json-object looks perfectly valid to me: http://jsbin.com/uwupa3/2/edit
$(document).ready(function(){
var timeService = "http://json-time.appspot.com/time.json?tz=Europe/Brussels";
$.getJSON(timeService, function(data) {
alert(data);
});
});
I get no JS-errors. I also tried this local (so not on JSbin but with a htm-file on my pc) and this doesn’t work either.
Can someone explain what I am doing wrong?
You are using an URL that’s outside your domain, which means
$.getJSONwill not use an XmlHttpRequest, but some JSONP — see the documentation of$.getJSON:And if you take a look on the documentation of the
jsonpoption for$.ajax, you’ll see :And for the
jsonpCallbackoption :For your first request, there is a
jsoncallbackparameter in the URL ; for your second request, there is no such parameter :http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?http://json-time.appspot.com/time.json?tz=Europe/BrusselsI suppose this has something to do with the fact the second request doesn’t do what you want ?