AJAX公司承诺使用数组 [英] AJAX Promises using Array

查看:154
本文介绍了AJAX公司承诺使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一些AJAX调用(假设2)使用的承诺。基本上,我希望能够在两个响应合并到一起,完成对他们的一些分析,作为一个整体,然后吐出一个响应。 现在,我有:

  VAR responseArray = [];
对于(VAR I = 0; I< letsSay​​Two;我++){
  responseArray.push(someAjaxCall(数据));
};
responseArray.done(函数(响应){
  VAR吐= someAnalysis(响应);
  执行console.log(吐);
});
responseArray.fail(函数(响应){
  的console.log('故障');
});
 

既然这样,我得到一个未捕获的类型错误:对象[对象数组]有没有方法'做'的错误控制台。我是正确的思考,我不能用这个方法?我看着使用code以下位来自( http://gregfranko.com/博客/ jQuery的最佳实践/ ),但我似乎无法得到我需要的响应。

  $。when.apply(这一点,responseArray)。然后(函数(响应){
  的console.log(响应);
});
 

相反,我所得到的是[回应,成功,回应]其中的第一反应是对AJAX的一个正确的返回响应呼吁,最后的反应是实际调用自身。我应该如何着手从两个AJAX调用??

得到正确的回复

我希望这一切是有道理的。谢谢!

解决方案
  

既然这样,我发现了一个未捕获的类型错误:对象[对象数组]有没有方法'做'在控制台错误。我是正确的思维,我不能用这个方法?

不是阵列上,是的。只能在承诺与延迟对象调用此方法,如通过 $产生的之一。when.apply(这一点,responseArray)

  

...但我似乎无法得到我需要的响应。相反,我所得到的是 [回应,成功,回应] ,其中第一反应是一个Ajax调用的最后响应正确的返回响应实际调用本身。

至于 $表示在文档。当 ,它解决了与多个参数的结果的承诺 - 当输入自己的承诺收益率做多值(如 $ AJAX 一样),每个参数都各自承诺分辨率的参数对象。你只得到了他们的第一个与响应,但也有 responseArray.length letsSay​​Two )的参数给回调。

  

我应该如何着手从两个AJAX得到正确答案的电话?

您想从每个的 参数 的对象,这样你就可以使用地图

  $。when.apply(这一点,responseArray).done(函数(){
  VAR响应= $ .MAP(参数,函数(参数){返回的args [0];})
      吐= someAnalysis(响应);
  执行console.log(吐);
}),失败(函数(jqXHR,textStatus,errorThrown){
  的console.log('失败:+ textStatus);
});
 

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');
});

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);
});

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!

解决方案

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?

Not on arrays, yes. You can call this method only on Promise and Deferred objects, like the one produced by $.when.apply(this, responseArray)

… 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.

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.

How should I go about getting the correct responses from both AJAX calls?

You want to extract the first item (response data) from each arguments object, so you can use 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆