jQuery doesn't return any API results in the Succes function of $.ajax() while using JSONP
I'm having a lot of trouble getting any results o开发者_Python百科ut of the JSONP datatype of the jQuery $.ajax() function. This is my jQuery code:
$('#show_tweets').click(function(){
$.ajax({
type: 'get',
url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jquery',
dataType: 'jsonp',
succes: function(data) {
$.each(data, function() {
$('<div></div>')
.hide()
.append('<img src="'+ this.profile_image_url +'"/>')
.append('<p>'+ this.text +'</p>')
.appendTo('#tweets_gallery')
.fadeIn();
})
},
error: function() {
alert('Iets gaat fout!!');
}
});
});
I've assigned the #show_tweets ID to a P tag. #tweets_gallery is an empty DIV. Now, I know I could also use the $.getJSON() function, but I just want to know how I can properly retrieve JSONP results with a $.ajax() request. I've clicked several times and it doesn't output anything.
Thanks in advance!
In case it's an error because of your typo:
succes: function(data)
should be success: function(data)
In reply to your comment below, the reason profile_image_url
is undefined is because there is no property in the root scope. The JSON returned from twitter is in the form of:
{
"text" : "The twitter text",
"user" : {
"profile_image_url" : "http://path.to.profileimage/",
...
},
...
}
So this.text
exists (which is why you get the value) but you should be accessing the profile image as this.user.profile_image_url
See the twitter API for how the JSON is structured.
精彩评论