使用数组的 AJAX 承诺 [英] AJAX Promises using Array
问题描述
我正在尝试使用 Promise 进行几次 AJAX 调用(假设为 2 个).基本上我希望能够将两个响应合并在一起,对它们作为一个整体进行一些分析,然后吐出一个响应.现在,我有:
I'm trying to make several AJAX calls (let's say 2) using promises. Basically I want to be able to merge the two responses together, perform some analysis on them as a whole, then spit out a response. Right now, I have:
var responseArray = [];
for (var i=0; i<letsSayTwo; i++) {
responseArray.push(someAjaxCall(data));
};
responseArray.done(function(response) {
var spit = someAnalysis(response);
console.log(spit);
});
responseArray.fail(function(response) {
console.log('fail');
});
就目前而言,我在控制台中收到未捕获的类型错误:对象 [对象数组] 没有方法‘完成’"错误.我认为我不能使用这种方法是否正确?我研究了使用以下代码 (http://gregfranko.com/blog/jquery-best-practices/) 但我似乎无法得到我需要的回应.
As it stands, I'm getting an "Uncaught TypeError: Object [object Array] has no method 'done'" error in console. Am I correct in thinking that I can't use this method? I looked into using the following bit of code from (http://gregfranko.com/blog/jquery-best-practices/) but I can't seem to get the response that I need.
$.when.apply(this, responseArray).then(function(response) {
console.log(response);
});
相反,我得到的是 [response, "success", response] 其中第一个响应是 AJAX 调用之一的正确返回响应,最后一个响应是实际调用本身.我应该如何从两个 AJAX 调用中获得正确的响应?
Instead, what I get is [response, "success", response] where the first response is the correct return response for one of the AJAX calls and the last response is the actual call itself. How should I go about getting the correct responses from both AJAX calls??
我希望这一切都有意义.谢谢!
I hope this all makes sense. Thanks!
推荐答案
就目前而言,我在控制台中收到
Uncaught TypeError: Object [object Array] has no method 'done'
错误.我认为我不能使用这种方法是否正确?
As it stands, I'm getting an
Uncaught TypeError: Object [object Array] has no method 'done'
error in console. Am I correct in thinking that I can't use this method?
不是在数组上,是的.您只能在 Promise 和 Deferred 对象上调用此方法,例如由 $.when.apply(this, responseArray)
Not on arrays, yes. You can call this method only on Promise and Deferred objects, like the one produced by $.when.apply(this, responseArray)
...但我似乎无法得到我需要的回应.相反,我得到的是 [response, "success", response]
其中第一个响应是一个 AJAX 调用的正确返回响应,最后一个响应是实际调用本身.
… but I can't seem to get the response that I need. Instead, what I get is
[response, "success", response]
where the first response is the correct return response for one of the AJAX calls and the last response is the actual call itself.
如 $.when
文档中所述,它使用多个参数解析结果承诺 - 当输入承诺本身确实产生多个值时(例如 $.ajax
确实如此),每个参数都是各自的承诺决议.您只使用 response
获得了第一个,但是回调有 responseArray.length
(letsSayTwo
) 参数.
As stated in the docs for $.when
, it resolves the result promise with multiple arguments - and when the input promises themselves did yield multiple values (such as $.ajax
does), each argument is an arguments object of the respective promise resolution. You were only getting the first of them with response
, but there are responseArray.length
(letsSayTwo
) arguments to the callback.
我应该如何从两个 AJAX 调用中获得正确的响应?
How should I go about getting the correct responses from both AJAX calls?
您想从每个arguments
对象,所以你可以使用 map
:
$.when.apply(this, responseArray).done(function() {
var responses = $.map(arguments, function(args) { return args[0]; }),
spit = someAnalysis(responses);
console.log(spit);
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log('fail: '+textStatus);
});
这篇关于使用数组的 AJAX 承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!