【发布时间】:2018-05-15 14:27:33
【问题描述】:
我想达到什么目的?
我想拦截 XMLHttpRequest 并修改某些特定请求的响应。 (例如,解密内容并将其分配给返回响应)
到目前为止我做了什么?
以下代码拦截请求并修改响应。它适用于除 IE 11 以外的所有浏览器(Chrome、Firefox、Opera、Edge)。
const dummySend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function () {
const _onreadystatechange = this.onreadystatechange;
this.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200 || this.status === 1223) {
// as response is read-only and configurable, make it writable
Object.defineProperty(this, 'response', {writable: true});
this.response = modifyResponse(this.response);
}
}
if (_onreadystatechange) {
_onreadystatechange.apply(this, arguments);
}
}
dummySend.apply(__self, arguments);
}
问题是什么?
所有这些仅在 IE 11 中不起作用,抛出的错误是“TypeError:在严格模式下不允许分配给只读属性”。
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: xmlhttprequest internet-explorer-11 interceptor