Ajax 循环和响应顺序
Ajax loop and response order
我正在使用 $.ajax.
从服务器 (geoserver) 检索一系列数据
(简化的)请求如下所示:
var dataList=[];
//var urllist= // a list of several URLs to request data from
$.each(urllist,function(i) {
$.ajax({
jsonpCallback: 'getJson',
type: 'GET',
url: urllist[i],
dataType: 'jsonp',
success: function(data) {
dataList[i]=data.value;
}
})
});
我需要写入全局变量 dataList
因为我需要在来自 urllist 的 all 请求完成后触发一个事件。 (我已经实现了像 so 这样的延迟)。
问题是完成的列表总是顺序不同。我需要结果与请求的顺序相同。
这可能是一个闭包问题,其中传递给 ajax 函数的索引 i
和稍后发生的对 dataList
的分配(当每个循环都继续进行)。
我试着像 this but the problem remains the same. Also $.each
那样处理,就像上面的代码一样,无论如何应该为每次迭代创建一个单独的闭包。
我已经设法实现了 recursive function 但它是同步的。
编辑:suggested duplicate 不处理循环 ajax 请求
您可以按正确顺序访问 $.when
回调中的所有结果
// map an array of promises
var deferreds = urllist.map(function(url){
// return the promise that `$.ajax` returns
return $.ajax({
url: url,
dataType: 'jsonp'
}).then(function(data){
return data.value;
})
});
$.when.apply($, deferreds).then(function(results){
// results will be array of each `data.value` in proper order
var datalist = results;
// now do whatever you were doing with original datalist
$.each(datalist....
}).fail(function(){
// Probably want to catch failure
}).always(function(){
// Or use always if you want to do the same thing
// whether the call succeeds or fails
});
问题与延迟无关,但与请求所需的 jsonp 或相关 jsonpcallback
有关。请求数据 json 解决了问题
感谢@charlietfl for the answer over at: Looped ajax request. Error handling and return order
对于查找此内容的任何人:您很可能必须启用 Cross-Origin Resource Sharing on geoserver 才能直接访问 JSON
我正在使用 $.ajax.
从服务器 (geoserver) 检索一系列数据(简化的)请求如下所示:
var dataList=[];
//var urllist= // a list of several URLs to request data from
$.each(urllist,function(i) {
$.ajax({
jsonpCallback: 'getJson',
type: 'GET',
url: urllist[i],
dataType: 'jsonp',
success: function(data) {
dataList[i]=data.value;
}
})
});
我需要写入全局变量 dataList
因为我需要在来自 urllist 的 all 请求完成后触发一个事件。 (我已经实现了像 so 这样的延迟)。
问题是完成的列表总是顺序不同。我需要结果与请求的顺序相同。
这可能是一个闭包问题,其中传递给 ajax 函数的索引 i
和稍后发生的对 dataList
的分配(当每个循环都继续进行)。
我试着像 this but the problem remains the same. Also $.each
那样处理,就像上面的代码一样,无论如何应该为每次迭代创建一个单独的闭包。
我已经设法实现了 recursive function 但它是同步的。
编辑:suggested duplicate 不处理循环 ajax 请求
您可以按正确顺序访问 $.when
回调中的所有结果
// map an array of promises
var deferreds = urllist.map(function(url){
// return the promise that `$.ajax` returns
return $.ajax({
url: url,
dataType: 'jsonp'
}).then(function(data){
return data.value;
})
});
$.when.apply($, deferreds).then(function(results){
// results will be array of each `data.value` in proper order
var datalist = results;
// now do whatever you were doing with original datalist
$.each(datalist....
}).fail(function(){
// Probably want to catch failure
}).always(function(){
// Or use always if you want to do the same thing
// whether the call succeeds or fails
});
问题与延迟无关,但与请求所需的 jsonp 或相关 jsonpcallback
有关。请求数据 json 解决了问题
感谢@charlietfl for the answer over at: Looped ajax request. Error handling and return order
对于查找此内容的任何人:您很可能必须启用 Cross-Origin Resource Sharing on geoserver 才能直接访问 JSON