js改写原生XMLHttpRequest进行全局http请求响应拦截

// 创建原始的XMLHttpRequest对象
        let oldOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function() {
            return oldOpen.apply(this, arguments);
        }
        let oldSend = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function() {
            this.addEventListener('readystatechange', function() {
                // 拦截response
                if (this.readyState === 4) {
                    try {
                        const data = this.response
                        console.log('拦截到的数据', data)
                    } catch (err) {
                        console.log('错误', err);
                    }
                    
                }
            }, false);
            return oldSend.apply(this, arguments);
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容