Javascript 监视/侦听Ajax readyState

Javascript 监视/侦听Ajax readyState,javascript,ajax,Javascript,Ajax,请参阅下面的代码: function createXMLHttpRequestObject() { // will store the reference to the XMLHttpRequest object var ajaxRequest; // create the XMLHttpRequest object try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLH

请参阅下面的代码:

function createXMLHttpRequestObject() {
    // will store the reference to the XMLHttpRequest object
    var ajaxRequest;
    // create the XMLHttpRequest object
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // return the created object or display an error message
    if (!ajaxRequest) alert("Error creating the XMLHttpRequest object.");
    else return ajaxRequest;
}


function ajax_update() {
  var ajaxRequest = createXMLHttpRequestObject();

  ajaxRequest.onreadystatechange = function(){

    if(ajaxRequest.readyState != 4){ 
    ...
    }

    if(ajaxRequest.readyState == 4){
    //process JSON data
    }
  }
}
我正在尝试从ajax_update()函数外部监视/侦听ajaxRequest.readyState值。 ajax_update()在单击按钮时触发

我的目标是在函数ajax_update()之外,仅在所有ajax调用完成时触发另一个JS函数,这意味着ajaxRequest.readyState==4

例如:

 <input type='button' value='SEND QUOTE' onclick=\"ajax_update(some params); function_that_fires_when_readystate_is_completed();\">

有什么想法吗

提前谢谢你

定义这个全局

 var requestCounter = 0;
然后

function ajax_update() {
  var ajaxRequest = createXMLHttpRequestObject();
  //after calling request.open();
    requestCounter++;

  ajaxRequest.onreadystatechange = function(){  

    if(ajaxRequest.readyState != 4){ 
          requestCounter--;
          //error code here
    }

    if(ajaxRequest.readyState == 4){
        //process JSON data
          requestCounter--;
    }

     if (requestCounter == 0)
          onAllRequestComplete();
  }
}


     function onAllRequestComplete(){
        // code that have to run when all requests have been completed
     }
希望有帮助。

使用回调

JS HTML


差不多吧。这真的很难看,但如果不知道你到底想做什么,我就不能给你一个更好的方法了。

我不确定我是否得到了这个问题。您已经在使用
readystate
属性了吗?您不能使用jQuery或其他javascript库吗?很遗憾,我不能使用jQuery.gdoron:我需要从ajax_update()外部监视readystate亚历山大:对于这种特殊情况:是的,只有1个ajax调用谢谢,但我想避免设置超时。。。而是使用事件侦听器。您是说使用jquery侦听readyState?或者用jquery重写ajax_update函数?在回答您的问题之前,让我先问一个问题。为什么您没有在onreadystatechange中定义代码,而定义了另一个函数来侦听准备好的响应?Behnam Esmaili:因为ajax_update()是一个通用函数,我不需要每次调用readyState时都听它。那么为什么不在请求完成后调用这个通用函数呢?
function ajax_update(callback) {
  var ajaxRequest = createXMLHttpRequestObject();
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
      callback();
    }
  };
  xmlhttp.open(...);
  xmlhttp.send(null);
}
<input onclick="ajax_update(function_that_listens_to_readyState);">
ajaxRequest1.onreadystatechange = function(){

    if(ajaxRequest.readyState != 4){ 
    ...
    }

    if(ajaxRequest.readyState == 4){
    //process JSON data
       firstIsReady  = true;
       check();
    }
  }

ajaxRequest2.onreadystatechange = function(){

    if(ajaxRequest.readyState != 4){ 
    ...
    }

    if(ajaxRequest.readyState == 4){
    //process JSON data
       secondIsReady  = true;
       check();
    }
  }

function check() {
    if (firstIsReady && secondIsReady) {
        //both ajax calls completed
    }
}