$ .getJSON never runs a success function

I use $ .getJSON to call the url and get JSON data at http://jsfiddle.net/9Desk/

Although JSON is retrieved from http://i.imgur.com/Smpgu.png , I cannot execute the success function.

$(function () { $.getJSON(url) .success(function (data) { alert(data); var listItems = ""; }); });​ 

Can someone tell me where I am wrong and why?

+4
source share
2 answers

You need to process the request as jsonp instead of regular json . To do this, simply define the callback as callback=? instead of callback=listPlaces . From the doc:

If the URL contains the string " callback =? " (Or similar, as defined by the server-side API), the request is processed as JSONP instead. See the discussion of the jsonp data type in $ .ajax () for more details.

Demo .

+4
source

From http://api.jquery.com/jQuery.getJSON/ , this is the correct format for getJSON:

 jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] ) 

url: a string containing the URL to which the request is sent.

: Card or line that is sent to the server with the request.

success (data, textStatus, jqXHR): a callback function that is executed if the request completes successfully.

0
source

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


All Articles