进行多个ajax调用时出错

时间:2015-12-19 17:52:10

标签: jquery ajax

我试图读取包含JSON数据的URL数组。我试图遍历数组并为每个URL加载JSON。但是我遇到了错误问题(Firebug中的网络错误429),这让我相信这是一个发送太多请求的问题。我修改并简化了我的代码,以便在jsfiddle中列出。我目前正在尝试使用setTimeout来延迟ajax调用,但在某些ajax调用之后我仍然遇到错误。

JSBIN http://jsfiddle.net/s7s4cx9y/3/

var dataURL = [
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380673/timeline?gameHash=ee3b8fd05dd64784",
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380609/timeline?gameHash=9dd207ff3ebfd598",
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380641/timeline?gameHash=828ef9a4dd73bd08", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380241/timeline?gameHash=23c53fb7fed21f53", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380327/timeline?gameHash=84db7440d6c8cfed", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380474/timeline?gameHash=d90ecc8a74124bcf",
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001390143/timeline?gameHash=f8a97528a608655c", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001390158/timeline?gameHash=05abd4c334651b0d", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001390159/timeline?gameHash=2bf6f0a9b1e84151", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001390160/timeline?gameHash=d4005554002839f3", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001410069/timeline?gameHash=39d2712050cd954a", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001410081/timeline?gameHash=12a38a55a9d5cf18", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001410080/timeline?gameHash=46fd67ed4c6dfb68", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440041/timeline?gameHash=aeba4cc81f453c31", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440036/timeline?gameHash=5debbe8da795a7cf", 
    "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440042/timeline?gameHash=53a746631e808796", "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440043/timeline?gameHash=4725b07311676885"
];
function test(url, x)
{
    $.ajax({
            url:url,
            dataType:'jsonp',
            async:false,

           error: function (parsedjson, textStatus, errorThrown) {
               $('body').append(
                    "<b>ERROR</b> </br> parsedJson status: " + parsedjson.status + '</br>' + 
                    "errorStatus: " + textStatus + '</br>' + 
                    "errorThrown: " + errorThrown + '</br></br>' + 
                    "URL: " + url + "<br> iterator: X = " + x + "</br></br>");       
           },
           success : function(data) {
               $('body').append(
                    "<b>SUCCESS</b> </br>" +
                    "URL: " + url + "</br> iterator: X = " + x + "</br></br>"); 
           }
     });
}
for(var x = 0; x < dataURL.length; x++)
{
    var proxyUrl = 'https://jsonp.afeld.me/';
    var url = proxyUrl + '?url=' +     encodeURIComponent(dataURL[x] ) + '&callback=?';
    setTimeout(test(url, x), 10000);
}

必须通过代理脚本重定向网址,以生成上一个问题中引用的正确jsonp:Passing JSON from server-side (.NET) to client-side (jQuery)

3 个答案:

答案 0 :(得分:1)

使用这种方式:

setTimeout(function(){ test(url, x) }, 10000);

否则立即调用test()方法:

setTimeout(test(url, x), 10000);

<强>更新

for(var x = 0; x < dataURL.length; x++)
{
    .....
    setTimeout(function(){ test(url, x) }, 10000*x);
    or
    setTimeout(function(){ test(url, x) }, 1000*x);
}

答案 1 :(得分:1)

这是一个递归解决方案和fiddle

var dataURL = [
"https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380673/timeline?gameHash=ee3b8fd05dd64784",
"https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380609/timeline?gameHash=9dd207ff3ebfd598",
"https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001380641/timeline?gameHash=828ef9a4dd73bd08"
];

function test(url) {
  return $.ajax({
    url:url,
    dataType:'jsonp',
    error: function (parsedjson, textStatus, errorThrown) {
      $('body').append(
      "<b>ERROR</b> </br> parsedJson status: " + parsedjson.status + '</br>' + 
      "errorStatus: " + textStatus + '</br>' + 
      "errorThrown: " + errorThrown + '</br></br>' + 
      "URL: " + url + "<br></br>");
    },
    success : function(data) {
      $('body').append(
      "<b>SUCCESS</b> </br>" +
      "URL: " + url + "</br></br>"); 
    }
});
}

(function iterateUrls() {
  var proxyUrl = 'https://jsonp.afeld.me/';
  var u = dataURL.shift();
  if(!u) { return; }
  var url = proxyUrl + '?url=' + encodeURIComponent(u) + '&callback=?';
  setTimeout(function() { 
    test(url).then(function() { iterateUrls(); });
  }, 1000);
})();

超级奖金编辑:

你根本不需要setTimeout!如果您一次只有一个请求在飞行中请求成功。这将为您节省大量时间:

(function iterateUrls() {
  var proxyUrl = 'https://jsonp.afeld.me/';
  var u = dataURL.shift();
  if(!u) { return; }
  var url = proxyUrl + '?url=' + encodeURIComponent(u) + '&callback=?';
  test(url).then(iterateUrls);
})();

您可以进行一些测试,看看您是否可以同时在线投放多个请求。

答案 2 :(得分:0)

您正在访问Riot Games API。它会发送429个响应a rate limiting measure以及Retry-After标题,以及您应该等待的时间。它还会针对缺失的匹配发送404响应,有时甚至包括应该存在但不能用于某些生产/缓存原因的匹配。

BTW,典型的Riot API开发人员计划成员获得了应包含在请求中的API密钥。每个密钥都有自己的速率限制。