Is it possible to catch the $.ajax beforeSend event for ALL ajax calls on the page regardless of where it's initiated?
Specifically I'm using Swagger UI / Swashbuckle and cannot modify the JS code on the page directly, but can use the InjectJavaScript call in the .EnableSwaggerUI call in SwaggerConfig.cs to inject some script. The swagger data is outside of my control and is not correctly formed to show the oauth settings, so I need to allow the user to enter the API key and have that placed in the header.
I've tried multiple iterations including:
$(document).on("ajaxStart", function (jqXHR) {
var key = $('#input_apiKey')[0].value;
if (key && key.trim() != "") {
key = "Bearer " + key;
jqXHR.setRequestHeader("Authorization", key);
}
});
and
$(document).on("beforeSend", function (jqXHR) {
var key = $('#input_apiKey')[0].value;
if (key && key.trim() != "") {
key = "Bearer " + key;
jqXHR.setRequestHeader("Authorization", key);
}
});
and
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
var key = $('#input_apiKey')[0].value;
if (key && key.trim() != "") {
key = "Bearer " + key;
jqXHR.setRequestHeader("Authorization", key);
}
}
});
All with zero success. Perhaps I've just been staring at the code too long and it's obvious, but I just can't get the function to be called.