Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未调用onreadystatechange的XMLHttpRequest原型_Javascript_Ajax_Uiwebview - Fatal编程技术网

Javascript 未调用onreadystatechange的XMLHttpRequest原型

Javascript 未调用onreadystatechange的XMLHttpRequest原型,javascript,ajax,uiwebview,Javascript,Ajax,Uiwebview,我试图在UIWebView中检测任何ajax调用何时结束。我修改了答案中的代码:尽我所能。以下是我的尝试: var s_ajaxListener = new Object(); s_ajaxListener.tempOnReadyStateChange = XMLHttpRequest.prototype.onreadystatechange; s_ajaxListener.callback = function () { window.location='ajaxHandler://'

我试图在UIWebView中检测任何ajax调用何时结束。我修改了答案中的代码:尽我所能。以下是我的尝试:

var s_ajaxListener = new Object();
s_ajaxListener.tempOnReadyStateChange = XMLHttpRequest.prototype.onreadystatechange;
s_ajaxListener.callback = function () {
    window.location='ajaxHandler://' + this.url;
};

XMLHttpRequest.prototype.onreadystatechange = function() {
    alert("onreadystatechange called");
    s_ajaxListener.tempOnReadyStateChange.apply(this, arguments);
    if(s_ajaxListener.readyState == 4 && s_ajaxListener.status == 200) {
        s_ajaxListener.callback();
    }
}
我正在将其注入webView,但警报从未触发。如果我在脚本的开头或结尾放置一个警报,它将触发,因此我相当确定没有语法错误


我不是JS的人,所以我希望这是一个小问题。

XMLHttpRequest中放置一个通用的
onreadystatechange
。原型对我不起作用。但是,您链接到的代码可以很容易地进行调整,以便在发生该事件时调用自定义函数:

var s_ajaxListener = {};
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
// callback will be invoked on readystatechange
s_ajaxListener.callback = function () {
    // "this" will be the XHR object
    // it will contain status and readystate
    console.log(this);
}

XMLHttpRequest.prototype.open = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempOpen.apply(this, arguments);
  s_ajaxListener.method = a;  
  s_ajaxListener.url = b;
  if (a.toLowerCase() == 'get') {
    s_ajaxListener.data = b.split('?');
    s_ajaxListener.data = s_ajaxListener.data[1];
  }
}

XMLHttpRequest.prototype.send = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempSend.apply(this, arguments);
  if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
  // assigning callback to onreadystatechange
  // instead of calling directly
  this.onreadystatechange = s_ajaxListener.callback;
}

尝试反转操作顺序:首先定义
XMLHttpRequest.prototype.onreadystatechange
,然后将引用分配给
s\u ajaxListener.tempOnReadyStateChange
@bfavaretto no beans.@bfavaretto可能我没有完全理解,你能在回答中给出一个代码示例吗?我意识到可能还有更多,我会先做一些测试。@Lance你明白了吗?如果是这样,你能分享你的答案吗?使用这个.addEventListener(“readystatechange”,function(){});可能比较干净可能是因为这个。attachEvent当时应该用作备用项,但您的回答仍然很好:)谢谢,我感谢您的反馈。如果我今天要回答这个问题,我可能只会选择addEventListener。因为addEventListener是由IE9+支持的,它显然是有道理的:)