2016-06-10 85 views
0

只要每個請求返回成功,我的代碼就能正常工作。但如果只有一次失敗,「永遠」不會失敗。如果只有一個失敗,總是不會觸發

我希望「永遠」總是要着火。到目前爲止,我沒有找到答案來解決我的問題。 「完整」(我用於單個請求)不起作用。 「那麼」既不。

var counter = { ok:0, fail:0 }; 

for(var i in toDoList) 
{ 
    spRequest[i] = $.ajax({...}) 
    .done(function() { counter.ok ++; }) 
    .fail(function() { counter.fail ++; }); 
} 

$.when.apply(this, spRequest) 
.always 
(
    function() 
    { 
    if (spResp.ok == paybackTable.length) 
    { 
     console.log("everything went just fine"); 
    } 
    else 
    { 
     console.log("there were some problems:"); 
     console.log("- Requests OK: " + counter.ok); 
     console.log("- Requests failed: " + counter.fail); 
    } 
    } 
); 

我「有一些問題」一節中永遠達不到

+0

這可能有所幫助: 在ajax中處理好錯誤,您的代碼將能夠執行$ .when.apply行。 http:// stackoverflow。com/questions/2810128/try-catch-with-jquery-ajax-request 使用window.onerror查看錯誤 – Destrif

回答

0

恕我直言有在你的代碼一些不準確。首先,當你呼叫$.when.apply(),在.when()括號之間時,你必須把「$」作爲第一個參數,然後你的數組spRequest。如果您使用jQuery.Deferred,則可以更好地控制Deferreds(ajax調用等)的最終結果。 因此,你推入spRequest數組你的Deferreds,這是儘快執行。 $.when.apply($,spRequest)等待所有的延期被解決或拒絕。 我特意在每一個ajax.fail()self.resolve()代替self.reject()是因爲兩個原因:

    你要計算你的Ajax失敗
  • ;
  • 如果在一系列遞延對象其中之一發生故障,$.when.fail()火災和我想的是不是你要搜索什麼,或者更好的,我想是存在的,以你的Ajax調用稱爲文件,並能正確調用。在另一方面,如果不是這樣,你有兩種可能性:

    • ajax.fail()使用self.reject();
    • 考慮到檢查的StatusCode在您的Ajax調用:

    $.ajax({ statusCode: { 404: function() { //resolve and counter ++ stuffs in here } } });

這是一個例子目標您的需求:

*的index.php *

<html> 
<head> 
<title>TEST DEFERRED</title> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
</head> 
<body> 
<h1>TEST DEFERRED</h1> 
<script src="js/test.js"></script> 
</body> 
</html> 

* test.js *

var counter = { ok:0, fail:0 } 
,toDoList = [1,2,3,4,5,6,7,8,9,10] 
,spRequest = []; 

for(var i in toDoList) 
{ 
    console.log(i, i&1); 
    if(i&1) 
    { 
    console.log('odd, request an ok ajax call'); 
    spRequest[i] = jQuery.Deferred(function(){ 
     console.log('function ok called!'); 
     var self = this; 

     $.ajax({ 
     url: 'ajax/ajax_return_ok.php', 
     async: true, 
     data : {}, 
     type: 'post', 
     dataType: 'json' 
     }).done(function(d) { 
     console.log(d); 
     if(d.is_error) 
     { 
      counter.fail ++; 
      self.resolve(); 
     } else { 
      counter.ok ++; 
      self.resolve(); 
     } 

     }).fail(function(j) { 
     console.log(j.responseText); 
     //# fail of ajax call 
     counter.fail ++; 
     self.resolve(); 
     }); 

    }); 
    } else { 
    console.log('odd, request an error ajax call'); 
    spRequest[i] = jQuery.Deferred(function(){ 
     console.log('function error called!'); 
     var self = this; 

     $.ajax({ 
     url: 'ajax/ajax_return_error.php', 
     async: true, 
     data : {}, 
     type: 'post', 
     dataType: 'json' 
     }).done(function(d) { 
     console.log(d); 
     if(d.is_error) 
     { 
      counter.fail ++; 
      self.resolve(); 
     } else { 
      counter.ok ++; 
      self.resolve(); 
     } 

     }).fail(function(j) { 
     console.log(j.responseText); 
     //# fail of ajax call 
     counter.fail ++; 
     self.resolve(); 
     }); 

    }); 
    } 

} 


jQuery.when.apply($,spRequest).done(function(){ 
    console.log('when done'); 
}).fail(function(){ 
    console.log('when fail'); 
}).always(function(){ 
    if (counter.fail === 0) 
    { 
    console.log("everything went just fine"); 
    } 
    else 
    { 
    console.log("there were some problems:"); 
    console.log("- Requests OK: " + counter.ok); 
    console.log("- Requests failed: " + counter.fail); 
    } 
}); 

* ajax_return_error.php *

<?php 
echo json_encode(array('is_error' => true,'error_message' => 'this is an error message')); 

* ajax_return_ok.php *

<?php 
echo json_encode(array('is_error' => false,'result' => 'ok')); 

道歉提前爲沒有創建小提琴。

希望我的回答有幫助。