多次调用XMLHttpRequest onreadystatechange

ped*_*res 17 php ajax

我正在php中实现一个登录系统并使用ajax请求.

这是我的要求

hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

hr.onreadystatechange = function() {

    var return_data = hr.responseText;
    if(hr.readyState == 4 && hr.status == 200) {
        alert('is logged');
    }else if (hr.status == 400){
        alert('is not logged');
    }
}


hr.send(vars); // Actually execute the request
Run Code Online (Sandbox Code Playgroud)

但当我收到回复时,浏览器会发出各种警报.任何人都可以帮忙解决这个问题吗?

TaL*_*han 33

是的,因为它应该....... onreadystatechange可以在一个请求中多次调用..并且警告两个条件是每当请求是好的时候200它会警告'它没有记录'并且当它获得readystate = 4以及OK信号它已登录

.readyState = 4 //请求已完成并且响应已就绪
状态= 200 //这意味着服务器成功处理了请求

因此,第二次警报弹出因为至少您的请求是成功地被骗了.欲了解更多信息:http: //www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

这是:


     xhrobj.onreadystatechange = function()
    {
      if (xhrobj.readyState == 4 && xhrobj.status == 200)
      {
        if (xhrobj.responseText)
         {
                //put your code here 
           document.write(xhrobj.responseText);
          }
       }
     };
Run Code Online (Sandbox Code Playgroud)