ES6+
XMLHttpRequest = (Parent =>
class XMLHttpRequest extends Parent {
constructor() {
const onload = () => {
this.removeEventListener('load', onload);
debugger;
};
super(...arguments);
this.addEventListener('load', onload);
}
}
)(XMLHttpRequest);
ES5
XMLHttpRequest = (function (Parent) {
XMLHttpRequest.prototype = Parent;
return XMLHttpRequest;
function XMLHttpRequest() {
var self = this;
Parent.apply(this, arguments);
this.addEventListener('load', function () {
self.removeEventListener('load', onload);
debugger;
});
}
})(XMLHttpRequest);
这应该让您在 Network 开发者工具中检查请求/响应。
注释01
ES5 语法不能被理解为 ES5+,主要是因为 function extends class ... 不是在运行时有效的东西。类部分将失败并显示Uncaught TypeError: Failed to construct 'XMLHttpRequest': Please use the 'new' operator。到时候,你可以考虑改变:
Parent.apply(this, arguments);
进入
super(...arguments);
注释02
这里的每个解决方案都假设发出 XHR 请求的库没有缓存 XMLHttpRequest 构造函数 [尚未]。
注释03
上述代码不适用于 ES5,尤其是 supper 与 .prototype 继承语法的 b/c。
为了规避这些问题,可以利用原型。在下面的代码中,我选择使用xhr.send 方法。
ES5+
XMLHttpRequest.prototype.send = (function (send) {
return function () {
this.addEventListener('load', function onload() {
this.removeEventListener('load', onload);
debugger;
});
return send.apply(this, arguments);
};
})(XMLHttpRequest.prototype.send);
也就是说,后一个代码不适用于让我深入研究的问题...主要是因为内部 Angular 代码之前注册了一个事件侦听器(完全重定向浏览器)甚至调用xhr.send 方法。不太好,嗯!?
就是这样!