Javascript 为请求禁用一些 jQuery 全局 Ajax 事件处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7436195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:16:09  来源:igfitidea点击:

Disabling some jQuery global Ajax event handlers for a request

javascriptjqueryajax

提问by Elias Zamaria

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 documentationmentions 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.

假设我定义了一些全局 Ajax 事件处理程序(ajaxStart、ajaxStop 和 ajaxError)。通常我对此没问题,但是对于一个请求,我想禁用 ajaxError 处理程序,但仍像往常一样运行 ajaxStart 和 ajaxStop 处理程序。在jQuery的AJAX功能的文件中提到,可以设置为false,并传递到$就功能禁用所有全局AJAX事件处理程序的“全球性”的参数,但他们没有提及任何办法只能禁用一些全球处理器。

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?

我可以通过对传递给 ajaxError 函数的 ajaxSettings 对象的“url”属性进行测试来阻止 ajaxError 处理程序,但这似乎有些笨拙。这里有没有人知道禁用 ajaxError 函数运行的方法,这对于查看 $.ajax 函数调用位置的人来说很清楚?

I can provide a simple example if anyone wants to see it.

如果有人想看,我可以提供一个简单的例子。

回答by Jon

It's possible and not difficult to do.

这是可能的,也不难做到。

You just need to setup your global error handler (.ajaxError) to receive a few of the parameters that jQuery can provide to it:

你只需要设置你的全局错误处理程序 ( .ajaxError) 来接收一些 jQuery 可以提供给它的参数:

$("div.log").ajaxError(function(evt, xhr, settings) {
    if(settings.suppressErrors) {
        return;
    }

    // Normal processing
});

After this, you can add suppressErrors: trueto the settings of any AJAX request you make, and if it fails the error handler will return without doing what it normally does.

在此之后,您可以添加suppressErrors: true到您发出的任何 AJAX 请求的设置中,如果失败,错误处理程序将返回而不执行通常的操作。

See it in action.

看到它在行动

回答by topek

When you look at this if statement from the jquery/ajax source https://github.com/jquery/jquery/blob/master/src/ajax.js#L579-582, which is one of many of the same kind, it is clear that your problem can not be solved by a jquery parameter alone.

当您查看 jquery/ajax 源https://github.com/jquery/jquery/blob/master/src/ajax.js#L579-582 中的if 语句时,它是许多同类中的一种,它很明显,您的问题不能仅通过 jquery 参数来解决。

My suggestion would be to:

我的建议是:

  • set global to false, like Vaibhav Gupta said

  • map your global handlers to local in the specific ajax call and trigger the global event through the $.event.trigger method

  • 将 global 设置为 false,就像 Vaibhav Gupta 所说的那样

  • 在特定的 ajax 调用中将您的全局处理程序映射到本地,并通过 $.event.trigger 方法触发全局事件

Sample:

样本:

$.ajax({
    url: "test.html",
    global: false,
    beforeSend: function(){$.event.trigger('ajaxStart');},
    complete: function(){$.event.trigger('ajaxStop');},
    error: your_error_handler
});

回答by Augustus Kling

You can set $.event.global.ajaxErrorto falseto disable invoking the global error callback temporarily. Use the errorand completecallbacks of your special request to set the flags:

您可以设置$.event.global.ajaxErrorfalse暂时禁用调用全局错误回调。使用您的特殊请求的errorcomplete回调来设置标志:

$.ajax({
    url: 'some.domain.com/',
}).error(function(jXHR){
    // Disable global error logging
    $.event.global.ajaxError = false;
}).complete(function(){
    // Enable global error logging
    $.event.global.ajaxError = true;
});

An example is available on jsFiddle.

jsFiddle上有一个示例。

That being presented, I want to add that I would still prefer Jon's proposal. This technique requires to set the flag at 2 places of which you could forget one and and up to have the global handlers disabled accidentally. The other reason is that Jon's technique should be guaranteed to work across jQuery releases where setting some internal flags is not reliable.

既然如此,我想补充一点,我仍然更喜欢 Jon 的提议。这种技术需要在 2 个地方设置标志,您可能会忘记其中一个,甚至更多的地方会意外禁用全局处理程序。另一个原因是应该保证 Jon 的技术在设置一些内部标志不可靠的 jQuery 版本中工作。

回答by Vaibhav Gupta

try to use global: falsein your ajax request like:

尝试global: false在您的 ajax 请求中使用,例如:

 $.ajax({
   url: "test.html",
   global: false,
   // ...
 });

source:: http://docs.jquery.com/Ajax_Events

来源:: http://docs.jquery.com/Ajax_Events

Look for Global Events.

寻找全球事件。