使用 JSONP 时,jQuery 不会在 $.ajax() 的 Succes 函数中返回任何 API 结果

发布于 2024-10-08 15:45:34 字数 892 浏览 0 评论 0原文

我在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2024-10-15 15:45:34

如果由于您的拼写错误而导致错误:

succes: function(data) 应该是 success: function(data)

在回复下面的评论时,原因 profile_image_url 未定义是因为根范围中没有属性。从 twitter 返回的 JSON 格式为:

{
  "text" : "The twitter text",
  "user" : {
           "profile_image_url" : "http://path.to.profileimage/",
           ...
  },
  ...
}

因此 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文