• <small id='NpFKm'></small><noframes id='NpFKm'>

  • <i id='NpFKm'><tr id='NpFKm'><dt id='NpFKm'><q id='NpFKm'><span id='NpFKm'><b id='NpFKm'><form id='NpFKm'><ins id='NpFKm'></ins><ul id='NpFKm'></ul><sub id='NpFKm'></sub></form><legend id='NpFKm'></legend><bdo id='NpFKm'><pre id='NpFKm'><center id='NpFKm'></center></pre></bdo></b><th id='NpFKm'></th></span></q></dt></tr></i><div id='NpFKm'><tfoot id='NpFKm'></tfoot><dl id='NpFKm'><fieldset id='NpFKm'></fieldset></dl></div>

      <legend id='NpFKm'><style id='NpFKm'><dir id='NpFKm'><q id='NpFKm'></q></dir></style></legend>

        <bdo id='NpFKm'></bdo><ul id='NpFKm'></ul>
    1. <tfoot id='NpFKm'></tfoot>

        在 JavaScript 中拦截 fetch() API 请求和响应

        时间:2023-10-15

      1. <small id='iRIeq'></small><noframes id='iRIeq'>

            <tbody id='iRIeq'></tbody>

            <legend id='iRIeq'><style id='iRIeq'><dir id='iRIeq'><q id='iRIeq'></q></dir></style></legend>
              <bdo id='iRIeq'></bdo><ul id='iRIeq'></ul>
              <tfoot id='iRIeq'></tfoot>

                1. <i id='iRIeq'><tr id='iRIeq'><dt id='iRIeq'><q id='iRIeq'><span id='iRIeq'><b id='iRIeq'><form id='iRIeq'><ins id='iRIeq'></ins><ul id='iRIeq'></ul><sub id='iRIeq'></sub></form><legend id='iRIeq'></legend><bdo id='iRIeq'><pre id='iRIeq'><center id='iRIeq'></center></pre></bdo></b><th id='iRIeq'></th></span></q></dt></tr></i><div id='iRIeq'><tfoot id='iRIeq'></tfoot><dl id='iRIeq'><fieldset id='iRIeq'></fieldset></dl></div>

                  本文介绍了在 JavaScript 中拦截 fetch() API 请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在 JavaScript 中拦截 fetch API 请求和响应.

                  I want to intercept fetch API requests and responses in JavaScript.

                  例如,在发送请求之前我要截取请求的 URL.我也想在响应到达后拦截它.

                  For example, before sending the request I want to intercept the request URL. I'd like to intercept the response once it arrives as well.

                  以下代码用于拦截所有XMLHTTPRequest的响应.

                  The below code is for intercepting responses of all XMLHTTPRequests.

                  (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);
                  

                  我想为 fetch() API 实现相同的功能.我该怎么做?

                  I want to implement the same feature for fetch() API. How can I do this?

                  推荐答案

                  为了拦截获取请求和参数,我们可以采用下面提到的方式.它解决了我的问题.

                  For intercepting the fetch request and parameter we can go for below mentioned way. its resolved my issue.

                   const constantMock = window.fetch;
                   window.fetch = function() {
                       // Get the parameter in arguments
                       // Intercept the parameter here 
                      return constantMock.apply(this, arguments)
                   }
                  

                  这篇关于在 JavaScript 中拦截 fetch() API 请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将本地文件中的 json 数据加载到 React JS 中 下一篇:可以使用 xhrFields 将 onprogress 功能添加到 jQuery.

                  相关文章

                  最新文章

                    1. <i id='Rhe1a'><tr id='Rhe1a'><dt id='Rhe1a'><q id='Rhe1a'><span id='Rhe1a'><b id='Rhe1a'><form id='Rhe1a'><ins id='Rhe1a'></ins><ul id='Rhe1a'></ul><sub id='Rhe1a'></sub></form><legend id='Rhe1a'></legend><bdo id='Rhe1a'><pre id='Rhe1a'><center id='Rhe1a'></center></pre></bdo></b><th id='Rhe1a'></th></span></q></dt></tr></i><div id='Rhe1a'><tfoot id='Rhe1a'></tfoot><dl id='Rhe1a'><fieldset id='Rhe1a'></fieldset></dl></div>

                      <small id='Rhe1a'></small><noframes id='Rhe1a'>

                    2. <tfoot id='Rhe1a'></tfoot>
                        <bdo id='Rhe1a'></bdo><ul id='Rhe1a'></ul>
                      <legend id='Rhe1a'><style id='Rhe1a'><dir id='Rhe1a'><q id='Rhe1a'></q></dir></style></legend>