No results are returned when using Flickr JSON request
I'm still fairly new to AJAX and I'm experimenting with Twitter and Flickr. Twitter开发者_Go百科 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.
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!
Try commenting this out:
$('<div></div>')
.hide()
.append('<h1>'+item.title+'</h1>')
.append('<img src="'+item.media.m+'" >')
.append('<p>'+item.description+'</p>')
.appendTo('#results')
.fadeIn();
And just do
console.log(data);
if you're using firebug (which you probably should be) or
alert(data);
That will tell you if your request is returning data. FYI, I ran curl on your url and it returns a bunch of json.
Does using dataType: 'json'
instead of dataType: 'jsonp'
make any difference?
'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.
精彩评论