【发布时间】:2019-09-25 13:14:56
【问题描述】:
我想为 ASP.Net Web 窗体应用程序中的每个 http 调用添加自定义标头(不记名令牌)。
使用以下链接中的建议,我添加了代码以将添加的标头发送到服务器,但无济于事。
How to intercept all http requests including form submits
和
How to alter the headers of a Request?
<script>
(function() {
(function (open) {
XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
console.log("Adding header");
open.call(this, method, url, async, user, password);
this.setRequestHeader("X-Hello", "There " + new Date());
};
})(XMLHttpRequest.prototype.open);
})();
</script>
和
<script>
(function() {
(function (send) {
XMLHttpRequest.prototype.send = function (data) {
console.log("Adding header");
this.setRequestHeader("X-Hello", "There");
send.call(this, data);
};
})(XMLHttpRequest.prototype.send);
})();
</script>
我了解该解决方案应该仅适用于 POST(但它不适用。)我确实看到了每个帖子的 console.log,但标题“X-Hello”从未在服务器端显示.
使用服务工作者的长解决方案失败了:
return Promise.resolve(new Request(data.url, data));
“构造'Request'失败:无法构造带有模式为'navigate'的Request和非空RequestInit的Request。”
【问题讨论】:
标签: javascript webforms xmlhttprequest http-headers