使用 Flickr JSON 请求时没有返回结果
我对 AJAX 还很陌生,并且正在尝试 Twitter 和 Flickr。 Twitter 到目前为止运行良好,但我在使用 Flickr API 时遇到了一些问题。
我没有得到任何结果。该 URL 似乎工作正常,我指向包含数组(“items”)的正确对象。有人可以告诉我我做错了什么吗?谢谢!
$('#show_pictures').click(function(e){
e.preventDefault();
$.ajax({
url: 'http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=home&nojsoncallback=1',
dataType: 'jsonp',
success: function(data) {
$.each(data.items, function(i, item){
$('<div></div>')
.hide()
.append('<h1>'+item.title+'</h1>')
.append('<img src="'+item.media.m+'" >')
.append('<p>'+item.description+'</p>')
.appendTo('#results')
.fadeIn();
})
},
error: function(data) {
alert('Something went wrong!');
}
});
});
编辑:我已经更改了 URL,并且在 FireFox 中收到错误报告:“无效标签”,涉及根范围中的“标题”对象。
I'm still fairly new to AJAX and I'm experimenting with Twitter and Flickr. Twitter is working fine so far, but I've run into some issues with the Flickr API.
I'm getting no results back. The URL seems to be working fine and I'm pointing to the right object containing the array ('items'). Can anybody tell me what I'm doing wrong please? Thanks!
$('#show_pictures').click(function(e){
e.preventDefault();
$.ajax({
url: 'http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=home&nojsoncallback=1',
dataType: 'jsonp',
success: function(data) {
$.each(data.items, function(i, item){
$('<div></div>')
.hide()
.append('<h1>'+item.title+'</h1>')
.append('<img src="'+item.media.m+'" >')
.append('<p>'+item.description+'</p>')
.appendTo('#results')
.fadeIn();
})
},
error: function(data) {
alert('Something went wrong!');
}
});
});
EDIT: I've changed the URL and I'm getting an error report back in FireFox: "Invalid label", regarding the "title" object in the root scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来问题出在 URL 上。显然,jQuery 总是需要一个回调参数,并且通常会附加“callback=?”。但是,由于 Flickr 使用“jsonpCallback”作为参数名称,因此我必须将 URL 更改为:
http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=home&jsoncallback=?
很快,它突然就像一个魅力!
It seems the problem was in the URL. Apparently, jQuery always needs a callback parameter and it usually appends "callback=?". However, since Flickr is using "jsonpCallback" as the name for the parameter, I had to change the URL to this:
http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=home&jsoncallback=?
And presto, it suddenly works like a charm!
尝试注释掉这一点:
那么就这样做
如果您正在使用 firebug (您可能应该这样做),
,或者这会告诉您您的请求是否正在返回数据。仅供参考,我在你的网址上运行了curl,它返回了一堆json。
Try commenting this out:
And just do
if you're using firebug (which you probably should be) or
That will tell you if your request is returning data. FYI, I ran curl on your url and it returns a bunch of json.
使用
dataType: 'json'
代替dataType: 'jsonp'
有什么区别吗?Does using
dataType: 'json'
instead ofdataType: 'jsonp'
make any difference?“无效标签”是指解释器期望的标签值对中的标签。可以这样解决
http://willcode4beer.com/tips.jsp?set=jsonInvalidLabel
或者你可以传递 jsoncallback=?返回的 json 用这些括号括起来。
'Invalid label' refers to the label from the label-value pairs the interpreter expects. It can be solved like so
http://willcode4beer.com/tips.jsp?set=jsonInvalidLabel
Alternatively you can pass jsoncallback=? for the the returned json to be wrapped with those parenthesis.