This is easy enough to work around, but it would be nice if it was wrapped up in the global ajax setup
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?
The beforeSend
callback takes two arguments: the XMLHTTPRequest
instance and the settings used by the current AJAX call.
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
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With