ajax通用错误处理和逐个案例

ajax通用错误处理和逐个案例

本文介绍了jQuery ajax通用错误处理和逐个案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $('html')ajaxError(function(e ,xhr,settings,exception){

var message ='';

if(xhr.status == 0){
message ='你离线!\\\
请检查你的网络';
}
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 {// POST调用错误处理
message = JSON.parse(xhr.responseText);
}

catch(ex){// GET调用错误处理
message = xhr.responseText;
}

}
else if(errStatus =='parsererror'){
message ='Error.\\\
Parsing JSON Request failed。

}
else if(errStatus =='timeout'){
message ='请求超时。
}
else {
message =('Unknown Error.\\\
'+ 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上提醒bar,我想提醒foo:

  error:function(xhr){
if(xhr.status == 404){
//window.location.href = $('#logon')。attr(' HREF');
alert(foo);
}
}

我还是坚持这样做?我不知道如何防止备份被触发,因为它们似乎都是触发的。

解决方案

我认为你可以用jQuery来控制它。在ajax调用期间发生的任何错误都调用全局ajaxError。但是,本地错误回调在全局回调之前被调用,所以您可以设置一个变量来告诉全局回调不要运行。



例如:

  var handlesLocally = false; 

$('html')。ajaxError(function(e,xhr,settings,exception){
if(!handlesLocally){
//运行正常的错误回调代码并且重置handleLocally
}
});

错误:function(){
//将managedLocally设置为true以让全局回调已被处理
handlesLocally = true;
}

您可以查看这个jsFiddle,显示如何完成(请务必点击顶部的点击链接):


I have a generic ajax Error handler written like so:

$('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(); }
        });
    }

});

So when the error is not 403 a dialog is shown with text relating to the error.

This is fine but what iwould like to do is have the generic handler as a backup and then deal with each error on an individual basis within the original ajax call.

so as the backup handler alerts "bar" on a 404 i would like to alert "foo" instead:

            error: function(xhr) {
            if (xhr.status == 404) {
                //window.location.href = $('#logon').attr('href');
                alert("foo");
            }
        }

I sthere anyway to do this? I don't know how to prevent the backup from firing as they both seem to trigger at the moment.

解决方案

I don't think you can control this with jQuery. The global ajaxError gets called on any error that happens during an ajax call. However, the "local" error callback gets called before the global callback so you could set a variable that tells the global callback not to run.

For instance:

var handledLocally = false;

$('html').ajaxError(function(e, xhr, settings, exception) {
    if (!handledLocally){
        //run the normal error callback code and the reset handledLocally
    }
});

error: function(){
    //set handledLocally to true to let the global callback it has been taken care of
    handledLocally = true;
}

You can view this jsFiddle that shows how this can be accomplished (be sure to click run at the top before clicking the links): http://jsfiddle.net/e7By8/

这篇关于jQuery ajax通用错误处理和逐个案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:00
查看更多