【发布时间】:2018-09-17 18:02:32
【问题描述】:
我一直在尝试重写这些方法以拦截 xhr 请求,但似乎我所做的一切都只为 this.readyState 打印“1”。 有人知道为什么吗?
addInterceptorsToXHRRequests() {
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
const originalStateChangeHandler = this.onreadystatechange;
this.onreadystatechange = function() {
console.log(' ---> ',this.readyState);
if (originalStateChangeHandler instanceof Function) {
originalStateChangeHandler.apply(this, arguments);
console.log('Ready state: ' + this.readyState);
}
};
return originalOpen.apply(this, arguments);
};
}
我从 index.js 调用这个函数,在 componentDidMount 生命周期方法的末尾。 (相当大的项目)
【问题讨论】:
标签: javascript reactjs xmlhttprequest