我有一個通用的Ajax錯誤處理程序編寫,像這樣:jQuery的AJAX通用的錯誤處理,並根據具體情況逐案
$('html').ajaxError(function(e, xhr, settings, exception) {
var message = '';
if (xhr.status == 0) {
message = 'You are offline!\n Please check your network.';
}
else if (xhr.status == 403) {
window.location.href = $('#logon').attr('href');
}
else if (xhr.status == 404) {
message = 'Requested URL not found.';
}
else if (xhr.status == 500) {
message = xhr.responseText;
$('#cboxLoadedContent div.news_article_content').append('<p>' + message + '</p>');
try {//Error handling for POST calls
message = JSON.parse(xhr.responseText);
}
catch (ex) {//Error handling for GET calls
message = xhr.responseText;
}
}
else if (errStatus == 'parsererror') {
message = 'Error.\nParsing JSON Request failed.';
}
else if (errStatus == 'timeout') {
message = 'Request timed out.\nPlease try later';
}
else {
message = ('Unknown Error.\n' + xhr.responseText);
}
if (message != '' && xhr.status != 500) {
message = message;
}
if (xhr.status != 403) {
$('#icis_dashboard').append('<p id="ajax_error_msg" class="offScreen">' + message + '</p>');
errorBox({
inline: true,
width: 0,
href: '#ajax_error_msg',
onLoadCall: function() { $('#cboxLoadedContent').jScrollPaneRemove(); },
onCleanupCall: function() { $('#ajax_error_msg').remove(); }
});
}
});
所以當誤差不是403一個對話框顯示文本有關錯誤。
這是不錯,但什麼想再去讀做的是具有通用處理器作爲備份,然後與每個錯誤處理原來的Ajax調用內的個別。
,從而在404的備份處理機警報「欄」我想提醒「富」而不是:
error: function(xhr) {
if (xhr.status == 404) {
//window.location.href = $('#logon').attr('href');
alert("foo");
}
}
我sthere反正做到這一點?我不知道如何防止備份開火,因爲他們現在似乎都觸發了。
確保你只要你做了`如果(handledLocally!)`檢查`handledLocally`變量重新設置爲false - 另外,你冒着多線程競爭的風險。 – Basic 2011-02-04 10:30:33
我在答案的評論中包含了重置,如果您看到jsfiddle,我將重置handlesLocally變量。是的,這是冒着多線程的風險,但不知道應用程序在做什麼,這將工作正常。 – 2011-02-04 20:50:10