【发布时间】:2015-05-23 11:31:11
【问题描述】:
我正在使用https://api.jquery.com/jquery.when/ 中描述的方法进行一系列 ajax 调用。 (基于该页面中的示例$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ))。
以下代码有效。但是,我无法弄清楚如何将数据数组传递给 .done() 方法。在下面的示例中,我有 data1、data2、data3。但是在实际情况下,它可能是 data1、data2、...、dataN,其中“N”可以是任意数字。你能帮我吗?我想将“i”值映射到相应的数据值。
function test() {
var myArr = [];
var i;
for (i = 1; i <= 3; i++) {
myArr.push(
jQuery.ajax({
type: "GET",
url: "http:/c.html/" + i,
});
);
}
$.when.apply($, myArr).done(function(data1, data2, data3) {
//do something on data1, data2, data3
}).fail (function (jqXHR, textStatus) {
//oops..failed
});
}
【问题讨论】:
标签: javascript jquery ajax