简体   繁体   English

当同步选项不可用时,如何等待ajax请求在javascript中完成?

[英]How to wait for ajax request to complete in javascript when synchronous option is not available?

I'm trying to wait for the AJAX request to complete. 我正在尝试等待AJAX​​请求完成。 It would be easy if the method xmlhttp.open would support async = false but Ant Galio does not support this option and only asynchronous requests are permitted. 如果方法xmlhttp.open支持async = false但Ant Galio不支持此选项,并且只允许异步请求,那将很容易。 The question is how can I wait for the callback to be called. 问题是我如何等待回调被调用。

    var ajaxFinished = false;
    var xmlhttp = new XMLHttpRequest();
    this.debug("-- onreadystatechange is being defined");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            ajaxFinished = true;
                var data = xmlhttp.responseText;
            if (xmlhttp.status == 200) {
                that.debug('downloadSettings: SUCCESS');
                [...]
            } else {
                that.debug('downloadSettings:');
                that.debug('-- Error: ');
                that.debug('-- ResponseText: "'+data+'"')
            }
        }
    }

    while (ajaxFinished == false) {

    }

    this.debug("-- open connection");
    xmlhttp.open("GET", requestUrl, true); /* Ant Galio does not support synchronous  */
    this.debug("-- send");
    xmlhttp.send();                 

I'm looking for some kind of active waiting. 我正在寻找某种积极的等待。 I know about another solution but I'm interested in a solution that would not require changing more of the code than is my example above. 我知道另一种解决方案,但是我对一种解决方案感兴趣,该解决方案与上面的示例相比,无需更改更多代码。

Thanks! 谢谢!

yes, you can 是的你可以

function getFile(url) {
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
     AJAX.open("GET", url, false);                             
     AJAX.send(null);
     return AJAX.responseText;                                         
  } else {
     return false;
  }                                             
}

var fileFromServer = getFile('http://somedomain.com/somefile.txt');

w3c definition http://www.w3.org/TR/XMLHttpRequest/#the-open()-method w3c定义http://www.w3.org/TR/XMLHttpRequest/#the-open()-方法

client . open(method, url [, async = true [, user = null [, password = null]]])

You can't. 你不能 There is no "active waiting" in JavaScript, there can be only one active execution a time ("single-threaded"). JavaScript中没有“活动等待”,一次只能有一个活动执行(“单线程”)。

There is a workaround. 有一种解决方法。 Instead of using the the blocking while loop for poll use the nonblocking setInterval ().. so your code might look something like this. 而不是使用非阻塞的while循环进行轮询,而是使用非阻塞的setInterval ()..这样您的代码可能看起来像这样。

    var ajaxFinished = false; 
    var xmlhttp = new XMLHttpRequest(); 
    this.debug("-- onreadystatechange is being defined"); 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4) { 
      ajaxFinished = true; 
      var data = xmlhttp.responseText; 
      if (xmlhttp.status == 200) { 
        that.debug('downloadSettings: SUCCESS'); 
         [...] 
      } else { 
         that.debug('downloadSettings:'); 
         that.debug('-- Error: "); 
         that.debug('-- ResponseText: "'+data+'"') 
      } 
     } 
    } 

    //Polling function 

    function checkEvent(){
    if(ajaxFinished == true){
    //your code i.e xmlhttp.open("GET", requestUrl, true);
    }
    clearInterval(chkeventid);//Clear Interval via ID for single time execution
    }

    var chkeventid=self.setInterval("checkEvent()",100);//The poll call

    The setInterval method is treated a bit differently in JS as you know so you may use it as against the while loop.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM