我正在尝试拦截使用HTTP进行的fetch调用。我读了一遍:
也尝试了同样的方法,但是我无法拦截来自fetch函数的请求。
以下是我正在做的事情:
(function(open) {
XMLHttpRequest.prototype.open = function() {
this.addEventListener("readystatechange", function() {
console.log(this.readyState);
}, false);
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
fetch("http://jsonplaceholder.typicode.com/posts")
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
})我接收到响应而不调用拦截器。我错过了什么?我想添加一个拦截到HTTP请求,无论应用程序是使用ReactJS,角还是普通JS创建的。
发布于 2019-10-11 07:43:58
我觉得你应该用这个来拦截接电话
const constantMock = window.fetch;
window.fetch = function() {
// Get the parameter in arguments
// Intercept the parameter here
console.log(arguments);
return constantMock.apply(this, arguments)
}在这篇文章中找到了link
https://stackoverflow.com/questions/58335773
复制相似问题