JS XMLHttpRequesst对象 http post的五种请求状态

记录一下js中对http请求的几种状态,下附代码

readyState

存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。

  • 0: 请求未初始化
  • 1: 服务器连接已建立
  • 2: 请求已接收
  • 3: 请求处理中
  • 4: 请求已完成,且响应已就绪
              //此处只做最基本的描述
              var xmlobj=new XMLHttpRequest();
               xmlobj.open("POST", "/godown/commit", true); //调用weather.php
                        xmlobj.setRequestHeader("cache-control", "no-cache");
                        xmlobj.setRequestHeader("contentType", "text/html;charset=uft-8") //指定发送的编码
                        xmlobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");  //设置请求头信息
                        //判断URL调用的状态值并处理
                        xmlobj.onreadystatechange = function () {
                            if (xmlobj.readyState == 4 && xmlobj.status == 200) {
                                alert("OK");
                            }
                            else
                            {
                                //注意观察此处,我们会发现多个状态
                                alert(xmlobj.status);
                            }
                        };  
                        var result = xmlobj.send(parm); //设置为发送给服务器数据
                        xmlobj = null;                    
原文地址:https://www.cnblogs.com/ives/p/6904544.html