禁用一些jQuery全局AJAX事件处理程序的请求 [英] Disabling some jQuery global Ajax event handlers for a request
问题描述
假设我有一些全局AJAX事件处理程序规定(ajaxStart,ajaxStop和ajaxError)。通常我很好的,但对于一个请求,我想禁用ajaxError处理,但仍运行ajaxStart和ajaxStop处理如常。该 jQuery的AJAX功能文档提到,可以设置为false,并传递到$就功能全局参数禁用所有全局AJAX事件处理程序,但他们不提任何办法只能禁用一些全球处理器。
Suppose that I have some global Ajax event handlers defined (ajaxStart, ajaxStop, and ajaxError). Usually I am fine with that, but for one request, I want to disable the ajaxError handler but still run the ajaxStart and ajaxStop handlers as usual. The jQuery ajax function documentation mentions the "global" parameter that can be set to false and passed to the $.ajax function to disable all global Ajax event handlers, but they don't mention any way to only disable some global handlers.
我可以prevent的ajaxError处理程序做测试的ajaxSettings的URL属性对象传递给ajaxError功能,但似乎有些笨拙。有谁知道这里的一种方式,从运行禁用ajaxError职能将是清楚有人找 在那里的$阿贾克斯函数被调用的地方?
I can prevent the ajaxError handler by doing a test on the "url" property of the ajaxSettings object that is passed to the ajaxError function, but that seems somewhat clumsy. Does anyone here know of a way to disable the ajaxError function from running that would be clear to someone looking at the place where the $.ajax function is being called?
我可以提供一个简单的例子,如果有人想看看吧。
I can provide a simple example if anyone wants to see it.
推荐答案
这是可能的,而不是困难的事情。
It's possible and not difficult to do.
您只需要设置你的全局错误处理程序(.ajaxError <$ C C $>)接收几个参数的jQuery可提供给它:
You just need to setup your global error handler (.ajaxError
) to receive a few of the parameters that jQuery can provide to it:
$("div.log").ajaxError(function(evt, xhr, settings) {
if(settings.suppressErrors) {
return;
}
// Normal processing
});
在此,您可以添加燮pressErrors:真正的
来的任何AJAX请求您的设置,如果失败的错误处理程序将返回而不做它一般不会。
After this, you can add suppressErrors: true
to the settings of any AJAX request you make, and if it fails the error handler will return without doing what it normally does.
See it in action.
这篇关于禁用一些jQuery全局AJAX事件处理程序的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!