ajax发送多个跨域请求回调不混乱

时间:2023-11-23 10:11:44
     var count = 0;
var codes = "";
function refreshCache(urls){
try {
var url = urls.split(",");
if(url.length <=0){
alert("刷新失败,请配置刷新地址!");
return;
}
count = 0;
refesh(url);
}catch(err){
alert("请求失败。");
}
} function refesh(url){
var u = url[count];
console.log("第"+count+"个"+u);
$.ajax({
url : u,
type : 'POST',
dataType: "jsonp",
timeout: 2000,
cache:false,
async:false,
jsonpCallback: "jsonpcallback",
success : function(data, status, xhr) {
console.log(u+"回调被执行了... 参数为:"+data);
if(data == '0000'){
console.log(u+"回调执行成功");
count++;
if(count <20 && count < url.length){
refesh(url);
}
}else{
console.log(u+"回调执行失败");
alert("刷新失败,请稍后再试");
}
},
error : function(xhr, error, exception) {
alert("请求失败。");
}
});
  }