jQuery.when:获取xhr状态码

jQuery.when: Get the xhr status code

我有两个 ajax 调用,在它们都使用 jQuery.when()

完成后执行一些回调
$.when(loadDataA(), loadDataB()).done(function(dataA, dataB) {
  console.log(xhr.status); // How do I get the xhr.status?
}

我想知道第一个电话的xhr.status。使用普通的 .done() 你可以做类似的事情:

.done(function(data, statusText, xhr) {
  console.log(xhr.status);
}

但我无法让它在我的情况下工作。

使用jQuery.when时如何获取?

来自 $.when() 的文档:

如果 Deferred 被解析为没有值,相应的 doneCallback 参数将是未定义的。如果 Deferred 解析为单个值,则相应的参数将保存该值。在 Deferred 解析为多个值的情况下,相应的参数将是这些值的数组。例如:

var d1 = $.Deferred();
var d2 = $.Deferred();
var d3 = $.Deferred();

$.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) {
    console.log( v1 ); // v1 is undefined
    console.log( v2 ); // v2 is "abc"
    console.log( v3 ); // v3 is an array [ 1, 2, 3, 4, 5 ]
});

d1.resolve();
d2.resolve( "abc" );
d3.resolve( 1, 2, 3, 4, 5 );

在您的例子中,dataA 将是一个包含三个元素的数组:

dataA[0] -> data
dataA[1] -> statusText
dataA[2] -> xhr

有了这个...

How do I get it when using jQuery.when?

dataA[2].status