weixin_33744141 2013-09-30 07:46 采纳率: 0%
浏览 5

使用数组的AJAX承诺

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!

  • 写回答

1条回答 默认 最新

  • weixin_33725270 2013-09-30 09:22
    关注

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改