jQuery的阿贾克斯通用错误处理,并根据具体情况逐案斯通、根据具体情况、错误、jQuery

2023-09-10 15:59:46 作者:想╭ァ莣汜

我有一个通用的ajax错误处理程序写的,像这样:

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

});

因此​​,当错误不可403被示为具有文本有关错误的对话框。

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

这是不错,但什么想再去读做的是具有通用处理器作为备份,然后与每个错误处理原来的Ajax调用内以个人为基础。

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.

这样一个404的备份处理警报栏我想提醒富,而不是:

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");    
            }
        }

我sthere反正要做到这一点?我不知道该怎么prevent备份烧制而成,他们似乎都触发的时刻。

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.

推荐答案

我不认为你可以用jQuery控制这一点。全球ajaxError被调用的Ajax调用过程中发生任何错误。然而,本地的错误回调被称为全局回调之前,所以你可以设置一个变量,它告诉世界回调不运行。

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.

例如:

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;
}

您可以查看此的jsfiddle,显示了这是如何实现的(一定要单击顶部点击链接之前运行): HTTP:/ /jsfiddle.net/e7By8/

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/