Reputation: 2222
I want to change the AJAX Header with $.ajaxPrefilter
and tried following:
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
delete options.headers["myOwnHeader1"];
options.headers["myOwnHeader2"] = "test";
});
In the Network Console (Chrome) the myOwnHeader2
is set and the myOwnHeader1
not. But it throws the following Exception: Uncaught TypeError: Cannot convert null to object
on options.headers["myOwnHeader2"] = "test";
Upvotes: 2
Views: 3480
Reputation: 665256
The options
object does not necessarily contain a headers
property - only if given in the global $.ajaxSettings
or in the current (original) options. So your choices are
$.ajaxSetup({headers: {}});
, maybe with the myOwnHeader2
already setif ("headers" in options)
blockoptions.headers = options.headers || {};
delete options.headers["myOwnHeader1"];
options.headers["myOwnHeader2"] = "test";
Upvotes: 1