Javascript XHR(Ajax)发送原型的奇怪行为
我试图否决ajax方法的send方法。请参见下面的我的代码:Javascript XHR(Ajax)发送原型的奇怪行为,javascript,jquery,ajax,Javascript,Jquery,Ajax,我试图否决ajax方法的send方法。请参见下面的我的代码: (function() { var beforeCall = window.XMLHttpRequest.prototype.open, sendCall = window.XMLHttpRequest.prototype.send; window.XMLHttpRequest.prototype.open = function() { console.log("OPEN"); //
(function() {
var beforeCall = window.XMLHttpRequest.prototype.open,
sendCall = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.open = function() {
console.log("OPEN"); // Works
// Only called once, but Ajax has 5 states, which are should log 5 times
this.onreadystatechange = function(){ console.log("READYSTATE SOMETHING"); }
return beforeCall.apply(this, [].slice.call(arguments));
};
window.XMLHttpRequest.prototype.send = function() {
var origCallback = this.onreadystatechange;
this.onreadystatechange = function(){
console.log("ONREADYSTATECHANGE"); // This log never gets called
if( origCallback ) {
origCallback.apply(this, [].slice.call(arguments))
}
};
return sendCall.apply(this, [].slice.call(arguments));
}
})();
打开被记录,READYSTATE只记录一次,但是应该记录每个状态吗?然后在我的发送函数中,我尝试再次覆盖它(应该在这里,因为可能我的网站已经有了onreadystatechange,我不想丢失该回调。因此,我需要它,但该回调永远不会被调用
原来的功能仍然适用于我上面的代码。但是我没有得到我想要的额外日志
有人知道为什么我的onreadystatechange和控制台.log(“onreadystatechange”)
从未被调用吗
我需要此代码的站点正在使用jQuery atm,但它也应该与本机Javascript Ajax配合使用。似乎在调用.apply(此,[].slice.call(参数))之前使用了
;
在FireFox中导致错误:
window.XMLHttpRequest.prototype.open = function() {
console.log("OPEN"); // Works
// Only called once, but Ajax has 5 states, which are should log 5 times
this.onreadystatechange = function(){ console.log("READYSTATE SOMETHING"); }
console.log('still ok here');
var ret = beforeCall.apply(this, [].slice.call(arguments));//error here
console.log('this is not happening');
return ret;//neither does this
};
它在Chrome 32.0.1700.107中工作
这可能是因为XMLHttpRequest是一个主机对象,而open函数的行为与普通JS函数的行为不一样,可能仍然相关
这可能只是FireFox的一个bug,但这意味着不可能在任何XMLHttpRequest上侦听任何open和send调用。您可以创建一个包装器并使用该包装器来调用XHR请求,但您只能从代码中捕获open和send,而不是从第三方代码中捕获
[更新]
看来我犯了个错误
在firebug控制台(Firefox33)中运行以下代码时,打开google.com对我来说是可行的,但在新的选项卡中不起作用;必须访问一个站点
(function() {
var beforeCall = window.XMLHttpRequest.prototype.open,
sendCall = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.open = function open() {
var ret;
console.log("OPEN");
this.onreadystatechange = function(){ console.log("READYSTATE SOMETHING"); }
console.log('open still working');
ret = beforeCall.apply(this, [].slice.call(arguments));
console.log('open is done');
return ret;
};
window.XMLHttpRequest.prototype.send = function send() {
console.log('this is send');
var origCallback = this.onreadystatechange,ret;
this.onreadystatechange = function(){
console.log("ONREADYSTATECHANGE");
if( origCallback ) {
origCallback.apply(this, [].slice.call(arguments))
}
};
console.log('send still working');
ret = sendCall.apply(this, [].slice.call(arguments));
console.log('send done');
return ret;
}
})();
var xhr = new XMLHttpRequest();
xhr.open('GET','/');
//calling xhr.open with wrong order of parameters gives a not so elegant error
//xhr.open('/','GET')
xhr.send();
更新了我的答案,在FireFox中出错。在FireFox 33.0中无法重新生成代码中的错误,除非在没有打开页面的新选项卡的控制台中运行。我很好奇我的答案中的更新代码在其他浏览器中会为您生成什么(我只有FireFox和Chrome)。