javascript 确认使用 jQuery.ajaxSettings.xhr
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11926484/
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
confirm use of jQuery.ajaxSettings.xhr
提问by BarryFanta
var _orgAjax = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function () {
var xhr = _orgAjax();
DoSomeLogging();
return xhr;
};
I've inherited the code above. Can someone confirm my understanding that this will override any $.ajax(...)
call anywhere else on the page and execute DoSomeLogging()
each time?
我继承了上面的代码。有人可以确认我的理解,这将覆盖$.ajax(...)
页面上其他任何地方的任何调用并DoSomeLogging()
每次执行吗?
I'm hunting a different bug but stumbled upon this and wanted to be sure I understand what this piece of legacy code does?
我正在寻找一个不同的错误,但偶然发现了这一点,并想确保我了解这段遗留代码的作用?
I'm not convinced this is good practice/use of the xhr
function anyway but interested to hear your opinions. We're using Jquery 1.7.1.
xhr
无论如何,我不相信这是该功能的良好实践/使用,但很想听听您的意见。我们使用的是 Jquery 1.7.1。
Lastly, out of curiosity, what if _orgAjax()
failed to return if an (async: false
) call, would the logging method even get reached?
最后,出于好奇,如果_orgAjax()
( async: false
) 调用未能返回,日志记录方法是否会被访问?
Thanks.
谢谢。
回答by Nope
Can someone confirm my understanding that this will override any $.ajax(...) call anywhere else on the page and execute DoSomeLogging() each time?
有人可以确认我的理解,这将覆盖页面上任何其他地方的任何 $.ajax(...) 调用并每次执行 DoSomeLogging() 吗?
Yes. This will define the global default behaviour unless overwritten in a local ajax implementation.
是的。这将定义全局默认行为,除非在本地 ajax 实现中被覆盖。
The more documented way of defining global settings is the use of ajaxSetup().
定义全局设置的更多文档方法是使用ajaxSetup()。
However, you are able to set default global behaviour using ajaxSettings
as well, though this particular way is not documented anywhere directly in the jQuery documentation.
但是,您也可以使用设置默认全局行为ajaxSettings
,尽管在 jQuery 文档中没有直接记录这种特殊方式。
See DEMO- With Success
看演示- 成功
Lastly, out of curiosity, what if _orgAjax() failed to return if an (async: false) call, would the logging method even get reached?
最后,出于好奇,如果在 (async: false) 调用时 _orgAjax() 未能返回,日志记录方法是否会被访问?
The settings are configuring the beforeSend
so the logging method will always be called regardless of the async setting as pointed out correctly by Esailija.
这些设置正在配置,beforeSend
因此无论Esailja正确指出的异步设置如何,都将始终调用日志记录方法。
See DEMO- with async: true
, logging is called before return
See DEMO- with async: false
, logging is also called before return
参见DEMO- with async: true
,在返回之前调用日志记录
参见DEMO- with async: false
,在返回之前也调用日志记录
However please note though, according to the ajax documentation:
但是请注意,根据ajax 文档:
As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is
deprecated; you must use the complete/success/error callbacks.
从 jQuery 1.8 开始,不推荐使用带有 jqXHR ($.Deferred) 的 async: false
;您必须使用完整/成功/错误回调。
I know you are using 1.7.1 but if something becomes deprecated there usually is a good reason and I don't see any harm in preparing for that.
我知道您使用的是 1.7.1,但如果某些东西被弃用,通常有一个很好的理由,我认为为此做准备没有任何坏处。
To configure ajax with logging on complete
by default:
要配置 ajaxcomplete
默认登录:
See DEMO- Configuring default logging for complete
for document.
I know that would make your logging being executed after and not before the send but that seems to be the way 1.8 prefers it I think.
请参阅演示-complete
为文档配置默认日志记录。我知道这会使您的日志记录在发送之后而不是在发送之前执行,但这似乎是我认为 1.8 更喜欢它的方式。
回答by ?ime Vidas
Hm, you could:
嗯,你可以:
$.ajaxSetup({
beforeSend: function () {
doSomeLogging();
}
});
That's certainly simpler than having to redefine $.ajaxSettings.xhr
.
这当然比必须重新定义更简单$.ajaxSettings.xhr
。