jQuery的阿贾克斯 - 全局设置。是否有可能知道是什么事件/元素触发Ajax调用?有可能、全局、元素、事件

2023-09-10 13:26:00 作者:心癌晚期@

这是很容易解决,但它会很好,如果它被包裹在全球阿贾克斯设置

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
        }
    });
});
 
精彩推荐
图片推荐