如何使用自定义实现覆盖jQuery ajax函数以进行错误回调

时间:2022-12-08 19:29:29

I want to override jQuery ajax function with a custom implementation for error callback so that in case of some specific errors the call is not transferred to the error block of ajax call.
Want I am thinking to do is that in custom implementation I will check for the status and if it matches I will not call the original error handler.

我想用错误回调的自定义实现覆盖jQuery ajax函数,以便在某些特定错误的情况下,调用不会传输到ajax调用的错误块。我想要做的是,在自定义实现中,我将检查状态,如果匹配,我将不会调用原始错误处理程序。

5 个解决方案

#1


3  

Found the answer

找到了答案

(function($){

// Save reference to actual jQuery ajax function
var $_ajax = $.ajax;

$.ajax = function(options){

     if (options.error) {

        // save reference to original error callback
        var originalErrorHandler = options.error;

        var errorHandlerContext = options.context ? options.context : $;

        // define a custom error callback           
        var customErrorHandler = function(xhr,status,error) {

            if(error != 'Some error'){  
                // call original error callback 
                originalErrorHandler.apply(errorHandlerContext, arguments);
            }

        };

        // override error callback with custom implementation           
        options.error = customErrorHandler;
    };

    // call original ajax function with modified arguments  
    $_ajax.apply($, arguments);
};

})(jQuery);

#2


1  

I ran into this problem today and decided I should improve on @Varun's answer to handle the case that the consumer uses the deferred to set the error handler instead of the callback. Here is what I came up with to handle both cases:

我今天遇到了这个问题并决定我应该改进@Varun的答案来处理消费者使用延迟设置错误处理程序而不是回调的情况。以下是我提出的处理这两种情况的方法:

var oldAjax = $.ajax;
var newAjax = function (options) {
    var originalErrorHandler = options.error;
    var errorHandlerContext = options.context ? options.context : $;

    var customErrorHandler = function (xhr, status, error) {
        if (xhr.status === 401) {
            //do something
        }
        else if (xhr.status === 403) {
            //do something
        }
        else {
            if (originalErrorHandler) {
                originalErrorHandler.apply(errorHandlerContext, arguments);
            }
        }
    };

    if (options.error) {
        options.error = customErrorHandler;
    };

    var deferred = oldAjax.apply($, arguments).error(customErrorHandler);
    var addErrorHandler = deferred.error;
    deferred.error = function (localHandler) {
        var newLocalHandler = function (xhr) {
            if (xhr.status !== 401 && xhr.status !== 403) {
                localHandler.apply(localHandler, arguments);
            }
        };
        addErrorHandler.call(addErrorHandler, newLocalHandler);
        return deferred;
    };
    return deferred;
};
$.ajax = newAjax;

#3


0  

jQuery has ways of overriding global ajax handlers. If you want to respond to custom error codes, for example, you could override ajaxComplete, check the status, and call a custom error handler, or the default error handler.

jQuery有覆盖全局ajax处理程序的方法。例如,如果要响应自定义错误代码,则可以覆盖ajaxComplete,检查状态,并调用自定义错误处理程序或默认错误处理程序。

https://api.jquery.com/ajaxComplete/

Or override ajaxError and do your own error handling logic:

或者覆盖ajaxError并执行自己的错误处理逻辑:

https://api.jquery.com/ajaxError/

I hope this helps!

我希望这有帮助!

#4


0  

If there is any server side error the control will definitely go to error callBack. What you can do in this scenario is filter the action that you want to perform according to your will.

如果有任何服务器端错误,控件肯定会转到错误callBack。您可以在此方案中执行的操作是根据您的意愿过滤您要执行的操作。

$.ajax({
    error:function(jqXHR, textStatus, errorThrown){
          if(errorThrown == "specificErrorYouLookingFor"){
                // Do the special error handling you wish to do.
          }else{
               // Do normal error handling.
          }    
    }
})

#5


0  

See ajaxSetup https://api.jquery.com/jquery.ajaxsetup/ if you want set errorHandler for all future Ajax requests

如果要为将来的所有Ajax请求设置errorHandler,请参阅ajaxSetup https://api.jquery.com/jquery.ajaxsetup/

$.ajaxSetup({
    error: function(event, jqXHR, ajaxSettings, thrownError) {
        // this callback called always on all ajax request errors
    }
});

Or if you want do it for one call:

或者,如果你想为一个电话做:

$.ajax({
    // similar
    error: function(event, jqXHR, ajaxSettings, thrownError) {

    }
});

#1


3  

Found the answer

找到了答案

(function($){

// Save reference to actual jQuery ajax function
var $_ajax = $.ajax;

$.ajax = function(options){

     if (options.error) {

        // save reference to original error callback
        var originalErrorHandler = options.error;

        var errorHandlerContext = options.context ? options.context : $;

        // define a custom error callback           
        var customErrorHandler = function(xhr,status,error) {

            if(error != 'Some error'){  
                // call original error callback 
                originalErrorHandler.apply(errorHandlerContext, arguments);
            }

        };

        // override error callback with custom implementation           
        options.error = customErrorHandler;
    };

    // call original ajax function with modified arguments  
    $_ajax.apply($, arguments);
};

})(jQuery);

#2


1  

I ran into this problem today and decided I should improve on @Varun's answer to handle the case that the consumer uses the deferred to set the error handler instead of the callback. Here is what I came up with to handle both cases:

我今天遇到了这个问题并决定我应该改进@Varun的答案来处理消费者使用延迟设置错误处理程序而不是回调的情况。以下是我提出的处理这两种情况的方法:

var oldAjax = $.ajax;
var newAjax = function (options) {
    var originalErrorHandler = options.error;
    var errorHandlerContext = options.context ? options.context : $;

    var customErrorHandler = function (xhr, status, error) {
        if (xhr.status === 401) {
            //do something
        }
        else if (xhr.status === 403) {
            //do something
        }
        else {
            if (originalErrorHandler) {
                originalErrorHandler.apply(errorHandlerContext, arguments);
            }
        }
    };

    if (options.error) {
        options.error = customErrorHandler;
    };

    var deferred = oldAjax.apply($, arguments).error(customErrorHandler);
    var addErrorHandler = deferred.error;
    deferred.error = function (localHandler) {
        var newLocalHandler = function (xhr) {
            if (xhr.status !== 401 && xhr.status !== 403) {
                localHandler.apply(localHandler, arguments);
            }
        };
        addErrorHandler.call(addErrorHandler, newLocalHandler);
        return deferred;
    };
    return deferred;
};
$.ajax = newAjax;

#3


0  

jQuery has ways of overriding global ajax handlers. If you want to respond to custom error codes, for example, you could override ajaxComplete, check the status, and call a custom error handler, or the default error handler.

jQuery有覆盖全局ajax处理程序的方法。例如,如果要响应自定义错误代码,则可以覆盖ajaxComplete,检查状态,并调用自定义错误处理程序或默认错误处理程序。

https://api.jquery.com/ajaxComplete/

Or override ajaxError and do your own error handling logic:

或者覆盖ajaxError并执行自己的错误处理逻辑:

https://api.jquery.com/ajaxError/

I hope this helps!

我希望这有帮助!

#4


0  

If there is any server side error the control will definitely go to error callBack. What you can do in this scenario is filter the action that you want to perform according to your will.

如果有任何服务器端错误,控件肯定会转到错误callBack。您可以在此方案中执行的操作是根据您的意愿过滤您要执行的操作。

$.ajax({
    error:function(jqXHR, textStatus, errorThrown){
          if(errorThrown == "specificErrorYouLookingFor"){
                // Do the special error handling you wish to do.
          }else{
               // Do normal error handling.
          }    
    }
})

#5


0  

See ajaxSetup https://api.jquery.com/jquery.ajaxsetup/ if you want set errorHandler for all future Ajax requests

如果要为将来的所有Ajax请求设置errorHandler,请参阅ajaxSetup https://api.jquery.com/jquery.ajaxsetup/

$.ajaxSetup({
    error: function(event, jqXHR, ajaxSettings, thrownError) {
        // this callback called always on all ajax request errors
    }
});

Or if you want do it for one call:

或者,如果你想为一个电话做:

$.ajax({
    // similar
    error: function(event, jqXHR, ajaxSettings, thrownError) {

    }
});