使用 JSONP 时,jQuery 不会在 $.ajax() 的 Succes 函数中返回任何 API 结果
我在从 jQuery $.ajax() 函数的 JSONP 数据类型中获取任何结果时遇到很多麻烦。这是我的 jQuery 代码:
$('#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!!');
}
});
});
我已将 #show_tweets ID 分配给 P 标签。 #tweets_gallery 是一个空 DIV。现在,我知道我也可以使用 $.getJSON() 函数,但我只想知道如何使用 $.ajax() 正确检索 JSONP 结果要求。我点击了好几次,但没有输出任何内容。
提前致谢!
I'm having a lot of trouble getting any results out 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果由于您的拼写错误而导致错误:
succes: function(data)
应该是success: function(data)
在回复下面的评论时,原因
profile_image_url
未定义是因为根范围中没有属性。从 twitter 返回的 JSON 格式为:因此
this.text
存在(这就是您获取该值的原因),但您应该以this.user.profile_image_url< 的形式访问个人资料图像/code>
请参阅 twitter API 了解 JSON 的结构。
In case it's an error because of your typo:
succes: function(data)
should besuccess: 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:So
this.text
exists (which is why you get the value) but you should be accessing the profile image asthis.user.profile_image_url
See the twitter API for how the JSON is structured.