在AJAX调用中首先执行哪些方法? [英] Which methods are executed first in an AJAX call?

查看:94
本文介绍了在AJAX调用中首先执行哪些方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习有关AJAX的知识,我对执行AJAX调用中的方法的顺序有些困惑.我看到太多变化了.例如

                function submitArticle() {                  

                try {
              //alert("yaay");
                    xhr = new XMLHttpRequest();
                  }
                  catch(e) {
                    try {
                      xhr = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(e) {
                      try {
                          xhr = new ActiveXObject("Msxml2.XMLHTTP");
                      }
                      catch(e) {
                        alert("Your Browser is not Supported");
                        return false;
                      }
                    }    

                  }
              var parameters = "postTitle=" + postTitle "&postDes=" + postDes + "&postCont=" + postDes;    
              xhr.open('POST', '/engine/engine.php', true);      
              xhr.send(parameters);                                  
              xhr.onreadystatechange = function() {
                  if(this.readyState === 4) {
                      if(this.status ===200) {
                         alert(this.responseText);
                      }
                      else {
                        alert("status" + this.status);
                       }
                  }
                  else {
                    alert("readyState" + this.readyState);
                  }
              }


          }

我的问题是,我看到了将 open send 方法放置在非常不同的位置的代码,例如在评估readyState值之后.这是正确的方法.我在不同的站点上进行了查找,我所看到的只是jquery教程,没有一个以代码执行的顺序进行解释.抱歉,这是一个非常愚蠢的问题,或者我的代码错误.

解决方案

在代码完成运行之后,当控制权返回事件循环时,Javascript只能调用onreadystatechange回调.

因此,只要在同步执行的同一单元中添加处理程序,就可以在发送请求之前或之后添加处理程序.

I have been learning about AJAX and I am a little confused on which order the methods inside an AJAX call are executed. I have seen too many variations. For example

                function submitArticle() {                  

                try {
              //alert("yaay");
                    xhr = new XMLHttpRequest();
                  }
                  catch(e) {
                    try {
                      xhr = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(e) {
                      try {
                          xhr = new ActiveXObject("Msxml2.XMLHTTP");
                      }
                      catch(e) {
                        alert("Your Browser is not Supported");
                        return false;
                      }
                    }    

                  }
              var parameters = "postTitle=" + postTitle "&postDes=" + postDes + "&postCont=" + postDes;    
              xhr.open('POST', '/engine/engine.php', true);      
              xhr.send(parameters);                                  
              xhr.onreadystatechange = function() {
                  if(this.readyState === 4) {
                      if(this.status ===200) {
                         alert(this.responseText);
                      }
                      else {
                        alert("status" + this.status);
                       }
                  }
                  else {
                    alert("readyState" + this.readyState);
                  }
              }


          }

My question is that I have seen code where the open and send methods are placed in a very different spot, like after evaluating the readyState value. Which is the right way to go. I have looked it up on different sites and all i see are jquery tutorials and none of them explained in which order the code will be executed. Sorry if this is a very stupid question or if my code is wrong.

解决方案

Javascript can only call the onreadystatechange callback when control returns to the event loop, after your code finishes running.

Therefore, it doesn't matter whether you add the handler before or after sending the request, as long as you add it in the same unit of synchronous execution.

这篇关于在AJAX调用中首先执行哪些方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆