我想拦截从我的网页发出的所有http请求,并向请求正文添加一个参数.我的页面包含表单 - 我还想捕获表单提交.我尝试过使用Jquery ajaxSend和Javascript的setRequestHeader,但两者都不适用于我.我该如何实现这一目标?
谢谢
我想拦截Javascript中的fetch API请求和响应.
例如:在发送请求之前要拦截请求URL并且一旦获得响应就想拦截响应.
以下代码用于拦截所有XMLHTTPRequest的响应.
(function(open) {
XMLHttpRequest.prototype.open = function(XMLHttpRequest) {
var self = this;
this.addEventListener("readystatechange", function() {
if (this.responseText.length > 0 && this.readyState == 4 && this.responseURL.indexOf('www.google.com') >= 0) {
Object.defineProperty(self, 'response', {
get: function() { return bValue; },
set: function(newValue) { bValue = newValue; },
enumerable: true,
configurable: true
});
self.response = 'updated value' //Intercepted Value
}
}, false);
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
Run Code Online (Sandbox Code Playgroud)
我想为Fetch()API实现相同的功能.
提前致谢..