【问题标题】:Twitter API - If Rate limit exceededTwitter API - 如果超出速率限制
【发布时间】:2011-12-22 16:40:54
【问题描述】:

我正在为自己开发一个 Twitter 网络应用程序。我正在检索最新的热门话题。

这是我的做法:

    $.ajax({
                      url: 'http://api.twitter.com/1/trends/1.json',
                      dataType: 'jsonp',
                       success: function(data){


                       $.each(data[0].trends, function(i){
                          $('div#trending').hide().append("<p><a href='"+data[0].trends[i].url+"'>"+data[0].trends[i].name+"</a></p>").fadeIn(1000);
                        //Cache into LocalStorage
                    localStorage["trending"+i] = data[0].trends[i].name; //Name
                    localStorage["trendurl"+i] = data[0].trends[i].url;//URL

                                });
                            }
});

但有时我在开发它时,超出了速率限制。

如何检测是否超过了速率限制?

我似乎无法检测到是否显示此错误:

{"error":"Rate limit exceeded. Clients may not make more than 150 requests per hour.","request":"\/1\/trends\/1.json"}

我试过了:

success: function(data){
  if(data[0].error != 'undefined'){
    //load localstorage cache
  }
}

但这似乎不起作用。 请帮忙。

谢谢:)

【问题讨论】:

    标签: javascript jquery twitter jsonp


    【解决方案1】:

    当您受到速率限制时,Twitter API 会发送一个 HTTP 400 状态代码,因此请检查:

    $.ajax({
        // ...
        statusCode: {
            400: function() {
                alert( 'rate limited.' );
            }
        }
    });
    

    还请注意,您的比较有点错误。当错误文本为 not 'undefined' 时,data[0].error != 'undefined' 将始终返回 true。因此,即使您受到速率限制,错误文本也不会是 'undefined',因此会成功。您可能要检查的是:

    if ( !data[0].error ) { // data[0].error is not null
        // ...
    }
    

    【讨论】:

    • 谢谢。我刚刚从谷歌代码中找到了 jquery-jsonp。我应该使用 ajax 还是 jsonp 调用?谢谢:)
    • 对不起,不知道,我自己不怎么用 JQuery。但我想说你也可以很容易地让它与$.ajax一起工作,所以没有真正需要另一个插件。
    【解决方案2】:

    尝试类似 $.ajax({..}).fail(function(){});

    $.ajax({..})
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });
    

    现在让我知道这是如何工作的。

    干杯, /马辛

    【讨论】:

      【解决方案3】:

      如果您不进行 OAuth 调用,则您将被限制为每小时 150 次调用。但是,有一个小解决方法对我有用。

      根据关于速率限制的 Twitter 页面 (http://dev.twitter.com/docs/rate-limiting),“速率限制适用于使用 HTTP GET 命令请求信息的方法。通常使用的 API 方法将数据提交到 Twitter 的 HTTP POST 不受速率限制,但现在某些方法受到速率限制。”

      由于 AJAX 调用的默认类型是“GET”,请尝试将您的类型显式更改为“POST”,如下所示:

      $.ajax({
           url: 'http://api.twitter.com/1/trends/1.json',
           type: 'POST',
           dataType: 'jsonp',
           success: function(data){
                    $.each(data[0].trends, function(i){
                         $('div#trending').hide().append("<p><a href='"+data[0].trends[i].url+"'>"+data[0].trends[i].name+"</a></p>").fadeIn(1000);
                          //Cache into LocalStorage
                         localStorage["trending"+i] = data[0].trends[i].name; //Name
                         localStorage["trendurl"+i] = data[0].trends[i].url;//URL
      
                     });
            }
      

      });

      希望这会有所帮助!

      詹姆斯

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 1970-01-01
        • 2013-05-12
        • 2014-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-21
        • 2018-08-27
        相关资源
        最近更新 更多