你如何挂钩所有使用Fetch Api的AJAX请求?以前我们可以做这样的事情来挂钩所有的XMLHttpRequest:
(function() {
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
console.log('request started!');
this.addEventListener('load', function() {
console.log('request completed!');
console.log(this.readyState); //will always be 4 (ajax is completed successfully)
console.log(this.responseText); //whatever the response was
});
origOpen.apply(this, arguments);
};
})();
Run Code Online (Sandbox Code Playgroud)
或者更好的是,如果你想添加上面的函数,你将如何挂钩所有Fetch Api和所有XMLHttpRequest AJAX请求?