【问题标题】:HTTP intercept not being invoked. What could be the reason for this?未调用 HTTP 拦截。这可能是什么原因?
【发布时间】:2020-02-08 15:47:30
【问题描述】:

我正在尝试拦截使用 fetch API 进行的 HTTP 调用。我通读了:

并且正在尝试同样的事情,但我无法拦截来自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);
    })

我在没有调用拦截器的情况下收到响应。我错过了什么?无论应用程序是使用 ReactJS、Angular 还是 vanilla JS 创建的,我都想为 HTTP 请求添加拦截。

【问题讨论】:

    标签: javascript ajax reactjs xmlhttprequest fetch


    【解决方案1】:

    我认为你应该使用它来拦截 fetch 调用

            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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 2021-09-01
      • 2012-01-01
      • 2020-09-09
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多