然后使用 $.when.apply 获取一系列响应

Getting an array of responses in then using $.when.apply

我有以下代码:

var requests = [];

var files = ['one.html', 'two.html', 'three.html'];

for(var i = 0; i<files.length; i++){
        requests.push($.get(files[i]));
}

$.when.apply(undefined, requests).
then(function(resultOne, resultTwo, resultThree){
    console.log(resultOne[0]);
})

我希望避免在 .then 中为每个响应定义变量,而是检索响应数组。

我该怎么办?

谢谢!

在每个函数作用域中,在其作用域中都有一个类似对象的特殊数组,称为 arguments,它对应于传递给函数调用的值。

您可以使用 arguments 对象并对其进行迭代

$.when.apply(undefined, requests).
then(function(resultOne, resultTwo, resultThree) {
  $.each(arguments, function(i, val) {
    console.log(val);//it will be another array with 3 values
    console.log(val[0]);//to get the data returned by the ajax request
  })
})