我想向 ASP.Net Web 窗体应用程序中的每个 http 调用添加自定义标头(不记名令牌)。
使用以下链接中的建议,我添加了将添加的标头发送到服务器的代码,但无济于事。
和
<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>
Run Code Online (Sandbox Code Playgroud)
和
<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>
Run Code Online (Sandbox Code Playgroud)
我知道该解决方案应该只适用于 POST(但它不适用。)我确实看到了每个帖子的 console.log,但标题“X-Hello”从未显示在服务器端。
使用 Service Worker 的长解决方案失败了:
return Promise.resolve(new Request(data.url, data));
Run Code Online (Sandbox Code Playgroud)
“无法构建‘请求’:无法使用模式为‘导航’的请求和非空的 RequestInit 构建请求。”