Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过javascript拦截所有AJAX(xhr和fetch)请求?_Javascript_Reactjs - Fatal编程技术网

如何通过javascript拦截所有AJAX(xhr和fetch)请求?

如何通过javascript拦截所有AJAX(xhr和fetch)请求?,javascript,reactjs,Javascript,Reactjs,我有要拦截的xhr和fetch类型的xhr请求和响应 获取API调用 我试过了 window.fetch = function() { // Intercept the request here } 及 但是我想编写一个通用函数来截取所有xhr请求和响应。默认情况下,fetch()不提供截取请求的方法,但找到解决方法并不困难。您可以覆盖全局获取方法并定义自己的拦截器,如下所示: fetch=(originalFetch=>{ 返回(…参数)=>{ const result=origina

我有要拦截的xhr和fetch类型的xhr请求和响应

获取API调用

我试过了

window.fetch =  function() {
 // Intercept the request here
}

但是我想编写一个通用函数来截取所有xhr请求和响应。

默认情况下,fetch()不提供截取请求的方法,但找到解决方法并不困难。您可以覆盖全局获取方法并定义自己的拦截器,如下所示:

fetch=(originalFetch=>{
返回(…参数)=>{
const result=originalFetch.apply(这是参数);
返回result.then(console.log('Request wassent'));
};
})(取回);
取('https://api.github.com/orgs/axios')
.then(response=>response.json())
。然后(数据=>{
console.log(数据)

});我已经为这两种情况编写了代码。我想知道我们是否有办法编写一个函数来拦截fetch和xhr调用。我还试图弄清楚如何在
XMLHttpRequest.prototype.send=function(){//在这里拦截请求//我想在这里读取所有请求头}
XMLHttpRequest.prototype.send = function() {
// Intercept the request here
}