我有一个通用的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