我想用$.ajaxPrefilter
更改AJAX标题并尝试以下操作:
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
delete options.headers["myOwnHeader1"];
options.headers["myOwnHeader2"] = "test";
});
在网络控制台(Chrome)中,myOwnHeader2
已设置且myOwnHeader1
未设置。但它会引发以下异常:Uncaught TypeError: Cannot convert null to object
options.headers["myOwnHeader2"] = "test";
答案 0 :(得分:1)
options
对象不一定包含headers
属性 - 仅在全局$.ajaxSettings
或当前(原始)选项中给出。所以你的选择是
$.ajaxSetup({headers: {}});
,可能已设置myOwnHeader2
if ("headers" in options)
块
options.headers = options.headers || {};
delete options.headers["myOwnHeader1"];
options.headers["myOwnHeader2"] = "test";