我已经制作了一个ajax调用拦截器来监听XMLHttpRequests.
我想修改XHR的请求标头.出于某种原因,我只能改变它们XMLHttpRequest.send().是否要及早修改它们XMLHttpRequest.open()?它抛出了我粘贴在下面的代码中的异常.
那么,为什么我会收到错误?
(function (open) {
XMLHttpRequest.prototype.open = function () {
this.setRequestHeader('foo', 'bar');
// InvalidStateError: An attempt was made to
// use an object that is not, or is no longer, usable
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
(function (send) {
XMLHttpRequest.prototype.send = function () {
this.setRequestHeader('foo', 'bar'); // OK
send.apply(this, arguments);
};
})(XMLHttpRequest.prototype.send);
for(var i = 0; i < 3; i++){
var rnd = Math.round(Math.random()*3+1),
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () { };
httpRequest.open('GET', '/echo/json?delay=' + rnd);
httpRequest.send();
}
Run Code Online (Sandbox Code Playgroud)
Hal*_*yon 12
如果你交换怎么办:
this.setRequestHeader('foo', 'bar');
open.apply(this, arguments);
Run Code Online (Sandbox Code Playgroud)
要得到:
open.apply(this, arguments);
this.setRequestHeader('foo', 'bar');
Run Code Online (Sandbox Code Playgroud)
它有效吗?
| 归档时间: |
|
| 查看次数: |
4582 次 |
| 最近记录: |