You want to use beforeSend
to modify the request before it is being sent. This is all covered in the documentation you've linked to.
The global ajaxSend
event will not help you tamper with the request. The closest thing to global you can get would be to is call ajaxSetup
, passing a beforeSend
option to be default for all subsequent ajax calls.
There appears to be no simple way of getting request headers from an XMLHttpRequest
object. Since I assume you're setting your logging headers yourself, however, you might be able to hook into the setting of these headers, and store an accessible reference to them:
XMLHttpRequest.prototype.readableHeaders = {};
XMLHttpRequest.prototype.proxiedSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
XMLHttpRequest.prototype.setRequestHeader = function(header, value) {
this.proxiedSetRequestHeader(header, value);
this.readableHeaders[header] = value;
};
In this manner, you should be able to directly inspect the jqXHR.readableHeaders
object for your specific logging header, in beforeSend
, and call setRequestHeader
once more, to truncate the string, if needed.
To retrieve the headers you need access to the underlying instance of XMLHttpRequest from jqXHR object. Use xhr()
function to retrieve the instance.
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
console.log( settings.xhr().readableHeaders );
}
});