[英]jQuery getJSON never enters its callback function
I've been sitting with this for hours now, and I cant understand why. 我已经坐了几个小时了,我不明白为什么。
q
is working. q
正在工作。 The URL does give me a proper JSON-response. 该网址确实为我提供了正确的JSON响应。 It shows up as objects and arrays and whatnot under the JSON tab under the Net-tab in Firebug and all is fine.
它显示为对象和数组,而在Firebug的“ Net”选项卡下的“ JSON”选项卡下显示为what,一切都很好。 I've also tried with other URLs that i know work.
我也尝试过使用其他我知道的网址。 Same thing happens.
同样的事情发生。
I have another function elsewhere in my tiny app, wihch works fine, and is pretty much exactly the same thing, just another API and is called from elsewhere. 我的微型应用程序中的其他地方有另一个函数,wihch可以正常工作,并且几乎是同一件事,只是另一个API,可以从其他地方调用。 Works fine, and the
data
variable is filled when it enters the getJSON-function. 工作正常,并且在进入getJSON-function时填充了
data
变量。 Here, data
never gets filled with anything. 在这里,
data
永远不会充满任何东西。
I've had breakpoints on every single line in Firebug, with no result. 我在Firebug的每一行都有断点,没有结果。 Nothing happens.
什么都没发生。 It simply reaches the getJSON-line, and then skips to the debugger-statement after the function.
它只是到达getJSON行,然后跳到该函数之后的调试器语句。
var usedTagCount = 10; var searchHits = 20; var apiKey = "a68277b574f4529ace610c2c8386b0ba"; var searchAPI = "http://www.flickr.com/services/rest/?method=flickr.photos.search&" + "format=json&api_key=" + apiKey + "&sort=interestingness-desc&per_page=" + searchHits + "&jsoncallback=?&nojsoncallback=1&tags="; var tagString = ""; var flickrImageData = new Array(); function search(query) { for(var i = 0; i < usedTagCount; i++) { tagString += query[i].key + ","; } var q = searchAPI + tagString; $.getJSON(q, function(data) { debugger; /* It never gets here! */ $.each(data.photos.photo, function(i, item) { debugger; flickrImageData.push(item); }); }); debugger; return flickrImageData; }
q
): q
)的示例: http://www.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=a68277b574f4529ace610c2c8386b0ba&sort=interestingness-desc&per_page=20&jsoncallback=?&tags=london,senior,iphone,royal,year,security,project,records,online,after,
I do wonder, since JSONView (the firefox plugin) cannot format it properly, that it isn't really JSON that is returned - the mime-type is text/html. 我确实想知道,由于JSONView(firefox插件)无法正确设置其格式,因此返回的不是真正的JSON-mime类型是text / html。 Firebug, however, interprets it as JSON (as i stated above).
但是,Firebug会将其解释为JSON(如上所述)。 And all the tag words come from another part of the app.
并且所有标记词都来自应用程序的另一部分。
I think you might need to remove the 我认为您可能需要删除
nojsoncallback=1
from your searchAPI string. 从您的searchAPI字符串中。
Flickr uses JSONP to enable cross domain calls. Flickr使用JSONP启用跨域调用。 This method requires the JSON to be wrapped in a json callback, the nojsoncallback=1 parameter removes this wrapping.
此方法要求将JSON包装在json回调中,nojsoncallback = 1参数删除此包装。
EDIT: Apparently it works with nojsoncallback=1, I got this piece of code to work for me. 编辑:显然它与nojsoncallback = 1一起工作,我得到了这段代码为我工作。 What jQuery version are you using?
您正在使用哪个jQuery版本? JSONP is only available from 1.2 and up.
JSONP仅在1.2及更高版本中可用。
This works for me (slight modifications): 这对我有用(稍作修改):
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
var usedTagCount = 1;
var searchHits = 20;
var apiKey = "a68277b574f4529ace610c2c8386b0ba";
var searchAPI = "http://www.flickr.com/services/rest/?method=flickr.photos.search&" +
"format=json&api_key=" + apiKey + "&sort=interestingness-desc&per_page="
+ searchHits + "&jsoncallback=?&nojsoncallback=1&tags=";
var tagString = "";
var flickrImageData = new Array();
function search(query) {
tagString = query;
var q = searchAPI + tagString;
$.getJSON(q, function(data) {
$.each(data.photos.photo, function(i, item) {
debugger;
flickrImageData.push(item);
});
});
}
search("cat");
</script>
When I try the url: http://www.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=a68277b574f4529ace610c2c8386b0ba&sort=interestingness-desc&per_page=10&tags=mongo 当我尝试网址时: http : //www.flickr.com/services/rest/? method = flickr.photos.search&format = json&api_key = a68277b574f4529ace610c2c8386b0ba&sort = interestingness-desc&per_page =10& tags= mongo
it returns data, as it should - try to change the getJSON to an $.ajax() and define a function jsonFlickrApi (data) with the code you have in you callback function. 它返回数据,如它应该的那样-尝试将getJSON更改为$ .ajax()并使用回调函数中的代码定义一个函数jsonFlickrApi(数据)。
If that don't work - please post code to at jsbin.com <- so we can try it live - so much easier to debug. 如果这样不起作用-请在jsbin.com上发布代码<-,以便我们可以尝试使用-调试起来非常容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.