我在jQuery中使用通常的鏈接延遲方法進行一系列連續的AJAX調用。第一次調用返回一個值列表,隨後的調用是使用返回的列表條目進行的。在第一次調用返回列表後,隨後的調用可以按任意順序完成,但必須一次完成一次。所以這是我使用的:繼續失敗的jQuery推遲鏈
$.when(callWebService()).then(
function (data) {
var looper = $.Deferred().resolve(),
myList = JSON.parse(data);
for (var i in myList) {
(function (i) {
looper = looper.then(function() { // Success
return callWebService();
},
function (jqXHR, textStatus, errorThrown) { // Failure
if (checkIfContinuable(errorThrown) == true)
continueChain();
else
failWithTerribleError();
});
})(i);
}
});
事實證明,後來的調用可能會失敗,但我仍然想要做其餘的調用。在我的上市,這就是創造性的僞代碼這一點是爲了做:
if (checkIfContinuable(errorThrown) == true)
continueChain();
else
failWithTerribleError();
如何在地球上我雖然實現continueChain?看起來,任何延期的失敗都會導致鏈條的其他部分也失敗。相反,我想記錄錯誤並繼續處理列表的其餘部分。
'continueChain'和'failWithTerribleError'做了什麼? – Joseph
請參閱http://stackoverflow.com/questions/27171161/handling-different-success-and-fail-states-for-multiple-ajax-call-using-deferred,http://stackoverflow.com/questions/28131082/ jquery-ajax-prevent-fail-in-a-deferred-sequential-loop – guest271314
@CobusKruger嘗試使用'.always()' – guest271314