1

我将 $.when 与多个请求一起使用。请考虑以下代码:

$.when(
    $.ajax({url: '/show/' + showId + '/details.json', type: 'GET', dataType: 'json', async: false}),
    $.ajax({url: '/show/' + showId + '/graphicAssets.json', type: 'GET', dataType: 'json', async: false}),
    $.ajax({url: '/graphicAssets/show/' + showId + '/files.json', type: 'GET', dataType: 'json', async: false}),
    $.ajax({url: '/graphicAssets/show/types.json', type: 'GET', dataType: 'json', async: false}),
    $.ajax({url: '/season/'+ showId +'/seasons.json', type: 'GET', dataType: 'json', async: false}),
    $.ajax({url: '/freebie/' + showId + '/freebies.json', type: 'GET', dataType: 'json', async: false})
)
.then(function(showDetailsData, showGraphicAssetsData, showFilesData, showTypesData, showSeasonsData, showFreebieData, status, xhr) {
    if (xhr.status == '302') location.reload();

    // Rest of the code goes here
}

现在,我要做的是检查 xhr 对象的响应状态。结果未定义。我该如何解决这个问题?非常感谢!

4

1 回答 1

2

如果将多个 deferred 传递给$.when,则then处理程序会收到一个数组,其中包含解析每个 deferred 的结果。因此,在您的情况下,您将获得 7 个数组,每个数组包含data, textStatus, jqXHR.

因此,在您的情况下,正确的用法如下:

$.when(
$.ajax({url: '/show/' + showId + '/details.json', type: 'GET', dataType: 'json', async: false}),
$.ajax({url: '/show/' + showId + '/graphicAssets.json', type: 'GET', dataType: 'json', async: false}),
$.ajax({url: '/graphicAssets/show/' + showId + '/files.json', type: 'GET', dataType: 'json', async: false}),
$.ajax({url: '/graphicAssets/show/types.json', type: 'GET', dataType: 'json', async: false}),
$.ajax({url: '/season/'+ showId +'/seasons.json', type: 'GET', dataType: 'json', async: false}),
$.ajax({url: '/freebie/' + showId + '/freebies.json', type: 'GET', dataType: 'json', async: false})
)
.then(function(showDetailsData, showGraphicAssetsData, showFilesData, showTypesData, showSeasonsData, showFreebieData) {
    if (showDetailsData[2].status == '302' 
        || showGraphicAssetsData[2].status == '302' 
        ... etc) location.reload();

    // Rest of the code goes here
}
于 2013-04-21T17:39:34.513 回答