GetJSON: why does 1 example work and another doesn't?

I have 2 examples of a function that retrieves json data and issues a warning.

In this example, everything is going well: 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 the second example does not display a warning. What for? The only difference is the service in which json is retrieved. Json-object looks absolutely fair 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 do not get JS errors. I also tried this local one (not JSbin, but with the htm file on my computer), and this also does not work.

Can someone explain what I'm doing wrong?

+4
source share
2 answers

You are using a URL that is outside of your domain, which means that $.getJSON will not use XmlHttpRequest, but some JSONPs - see the $.getJSON documentation :

If the specified URL is on a remote server, the request is processed as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.


And if you look at the jsonp option jsonp for $.ajax , you will see:

Cancel the name of the callback function in the jsonp request. This value will instead of the “callback” in the “Callback =? Part of the query string in the URL for GET or data for AFTER. Thus, {jsonp: 'onJsonPLoad'} result in 'onJsonPLoad =?' switched to the server.

And for the jsonpCallback option:

Specify the name of the callback function for the jsonp request. This value will be instead of a random name automatically generated by jQuery.

For your first request, the url has a jsoncallback parameter; for your second request there is no such parameter:

  • First URL: http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?
  • Second URL: http://json-time.appspot.com/time.json?tz=Europe/Brussels

I suppose this is due to the fact that the second query does not do what you want?

+8
source

You are using a URL that is outside your domain, which means that $.getJSON will not use XmlHttpRequest , but some JSONP - see the documentation for $ .getJSON:

If the specified URL is on a remote server, the request is processed as JSONP instead. See the discussion of the jsonp data type in $ .ajax () for more details.

And if you look at the jsonp option documentation for $ .ajax, you will see:

Cancel the name of the callback function in the jsonp request. This value will be used instead of the "callback" in the "callback =?" part of the query string in the URL for GET or data for POST. So {jsonp: 'onJsonPLoad'} will result in 'onJsonPLoad =?' went to the server.

And for the jsonpCallback option:

+1
source

Source: https://habr.com/ru/post/1301543/


All Articles