就目前而言,我
Uncaught TypeError: Object [object Array] has no method'done'在控制台中遇到错误。我认为我不能使用此方法是否正确?
不是在数组上,是的。您只能在Promise和Deferred对象上调用此方法,例如由
$.when.apply(this, responseArray)
…但是我似乎无法获得所需的回复。相反,我得到的是
[response, "success",response]第一个响应是其中一个AJAX调用的正确返回响应,而最后一个响应是实际调用本身。
如docs
$.when中所述,它使用 多个参数
解析结果promise-
当输入promise本身确实产生多个值(例如,
$.ajax确实)时,每个参数都是各自promise解析的参数对象。您只能使用来获得第一个参数,但是回调
response有
responseArray.length(
letsSayTwo)个参数。
我应该如何从两个AJAX调用中获得正确的响应?
您要从每个
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);});


