2013-04-21 1 views
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 개체의 응답 상태를 확인하고 싶습니다. 결과는 정의되지 않습니다. 이 문제를 어떻게 해결할 수 있습니까? 많은 감사합니다!

답변

2

$.when에 여러 지연을 전달하면 then 처리기는 지연된 각 결과를 포함하는 배열을 수신합니다. 따라서 귀하의 경우 각각 data, textStatus, jqXHR을 포함하는 7 개의 배열을 얻게됩니다.

$.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 
} 
+0

대 : 귀하의 경우에 따라서

, 올바른 사용법은 다음과 같을 것입니다! 나는 내 질문에 답하려고했으나 너는 나아 갔다. 동의합니다. :) – user1966211