简体   繁体   中英

$.getJSON is not working with my website

I'm trying to make cross-domain request with $.getJSON to my site, and it's not working: callback function is not calling. I found this simple example:

 $.getJSON('http://twitter.com/users/usejquery.json?callback=?', function(data) { 
  console.log(data); 
 });

And it works with twitter, but not with my site. I copied the file is returned, the server response headers from Twitter, and it still not working. I do not know how to debug it and find out what the problem.

Anyone know what can it be?

Do you return JSONP ? The trick with the callback parameter is, that it's not JSON , that's returned, but a JavaScript function call with the "JSON" as parameter. Look at the result of the mentioned Twitter API call for an example.

The jQuery.ajax() docs also talk briefly about this (search for dataType and JSONP).

As to why a copy of the Twitter response does not work: The callback=? part of the getJSON call will be filled with a random function name. jQuery is making up the name as to not collide with any already existing function, and is deleting the function afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM