jQuery的阿贾克斯 - 全局设置。是否有可能知道是什么事件/元素触发Ajax调用? [英] jquery ajax - global settings. Is it possible to know what event/element trigger the ajax call?
问题描述
这是很容易解决,但它会很好,如果它被包裹在全球阿贾克斯设置
This is easy enough to work around, but it would be nice if it was wrapped up in the global ajax setup
当我运行一个Ajax调用,我想知道哪些元素/事件触发的beforeSend选项的AJAX调用。
When I run an ajax call, I'd like to know which element/event trigger the ajax call in the beforeSend option.
是否有这样做的简洁的方式?
Is there a concise way of doing this?
推荐答案
在 beforeSend
的回调有两个参数: XMLHTT prequest
实例,并用当前AJAX调用的设置。
The beforeSend
callback takes two arguments: the XMLHTTPRequest
instance and the settings used by the current AJAX call.
因此,如果您通过触发因素和事件的上下文
选项,他们将提供给 beforeSend
即使你在全局设置定义它:
Therefore, if you pass the triggering element and event in the context
option, they will be available to beforeSend
even if you define it in the global setup:
$.ajaxSetup({
beforeSend: function(xhr, settings) {
var element = settings.context.element;
var event = settings.context.event;
// Do something with 'element' and 'event'...
}
});
$("selector").click(function(e) {
$.ajax("url", {
// your settings,
context: {
element: this,
event: e
}
});
});
这篇关于jQuery的阿贾克斯 - 全局设置。是否有可能知道是什么事件/元素触发Ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!