admin管理员组文章数量:1290295
We can modify the XMLHttpRequest.prototype.open
to hijack all Ajax requests before. What's the equivalent if switching to the new browser's fetch API?
const originalRequestOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', function() {
// do something
});
originalRequestOpen.apply(this, arguments);
};
We can modify the XMLHttpRequest.prototype.open
to hijack all Ajax requests before. What's the equivalent if switching to the new browser's fetch API?
const originalRequestOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', function() {
// do something
});
originalRequestOpen.apply(this, arguments);
};
Share
Improve this question
asked Aug 17, 2016 at 11:46
Ivor ZhouIvor Zhou
1,27314 silver badges22 bronze badges
1
-
Patch
fetch
function? – Yury Tarabanko Commented Aug 17, 2016 at 11:53
1 Answer
Reset to default 9I don't remend to modify native objects and functions (even the way you did with XMLHttpRequest.prototype.open
). But you can replace fetch
function itselt. In the end it is just a function.
(function(ns, fetch) {
if (typeof fetch !== 'function') return;
ns.fetch = function() {
var out = fetch.apply(this, arguments);
// side-effect
out.then(({ ok }) => console.log('loaded', ok));
return out;
}
}(window, window.fetch))
fetch('https://jsonplaceholder.typicode./users')
fetch('https://jsonplaceholder.typicode./userz')
本文标签: javascriptHow to listen on all fetch API callsStack Overflow
版权声明:本文标题:javascript - How to listen on all fetch API calls? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741497046a2381882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论