gpt4 book ai didi

jquery - 多次ajax调用时出错

转载 作者:行者123 更新时间:2023-12-01 01:00:33 25 4
gpt4 key购买 nike

我正在尝试读取包含 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);
}

URL 必须通过代理脚本重定向才能生成正确的 jsonp,如上一个问题中所述:Passing JSON from server-side (.NET) to client-side (jQuery)

最佳答案

尝试在 $.ajax() 选项中设置 async: true ;在 IIFE 中包装对 setTimeout 的调用

for (var x = 0; x < dataURL.length; x++) {
(function(y) {
// `y` : `x`
var proxyUrl = "https://jsonp.afeld.me/";
var url = proxyUrl + "?url="
+ encodeURIComponent(dataURL[y]) + "&callback=?";
setTimeout(function() {
test(url, y)
}, y * 999999);
}(x))
}

jsfiddle http://jsfiddle.net/s7s4cx9y/4/

关于jquery - 多次ajax调用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com