简体   繁体   English

使用Ajax Send时等待响应加载

[英]Waiting for the response to load when Using Ajax Send

Hi I'm sending a request and receiving a response from the server using a javascript, 嗨,我正在使用javascript发送请求并从服务器接收响应,

xxmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
myotherMethod();

what I want to do is, wait till the response is fully loaded for the next instructions to be executed, How can I achieve this, thanks in advance. 我想做的是,等到响应完全加载后再执行下一条指令,我要如何实现这一点,请先感谢。

use onreadystatechange 使用onreadystatechange

xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readystate==4 && xmlhttp.status==200){
myotherMethod();
}
}
xmlhttp.send();

In jQuery you are able use async: false like this, Right now you are using core javascript so i don't have idea about it. 在jQuery中,您可以使用async:像这样的false,现在您正在使用核心javascript,所以我对此一无所知。 but if you will try jQuery and any query related to this then ask me. 但如果您尝试使用jQuery以及与此相关的任何查询,请询问我。

After add async: false then: after response alert('b') execute.. Other wise it will execute without response. 添加异步之后:false然后:在响应alert('b')执行之后。否则,它将在没有响应的情况下执行。

   jQuery.ajax({
           type: "POST",
           url: 'www.abc.com/1'
           async: false,
           success: function(returnHtml) {
               alert('a');
           }
               alert('b');
     });

Use onreadystatechange , readyState and status , try this: 使用onreadystatechangereadyStatestatus ,尝试以下操作:

xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      // response is fully loaded, write your next instructions here
  }
};

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

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