如何使用自定义实现覆盖jQuery ajax函数以进行错误回调 [英] How to override jQuery ajax function with a custom implementation for error callback

查看:73
本文介绍了如何使用自定义实现覆盖jQuery ajax函数以进行错误回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用错误回调的自定义实现覆盖jQuery ajax函数,以便在遇到某些特定错误的情况下,调用不会转移到ajax调用的错误块。

想要我想在自定义实现中我将检查状态,如果匹配,我将不会调用原始错误处理程序。

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.

推荐答案

找到答案

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

})(jQuery);

这篇关于如何使用自定义实现覆盖jQuery ajax函数以进行错误回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆