onreadystatechange func。不工作

时间:2017-08-08 15:05:04

标签: javascript ajax onreadystatechange

我写了一些关于ajax代码但是onreadystatechange函数。在chkPwd脚本func中没有工作....浏览器上只显示hiii 1的警告框



function chkPwrd(){
var fname = document.getElementById("fname").value;
var lname= document.getElementById("lname").value;
var umail= document.getElementById("umail1").value;
var upass= document.getElementById("upass").value;
var xhr=new XMLHttpRequest();
     if( flag === 1)
    {
    xhr.open("POST","conCheck?umail="+umail);
    xhr.send(null);
    alert("HIIII 1");
    xhr.onreadystatechange=function()
    {alert("HIIII 2");
         if(xhr.readyState===4 & xhr.status===200)
                    {alert("HIIII 3");
                        var a=xhr.responseText;
                    if(a.indexOf('5')!==-1)
                     {alert("Emailid already Exists");
            document.getElementById('umail1').style.color="red";
             // document.getElementById('umail1').innerHTML="Emailid already Exists";
                     }
                     if(a.length===0)
  {alert("Registering you..please click OK");
      var char="register.jsp?fname="+fname+"&lname="+lname+"&umail="+umail+"&upass="+upass;
  window.open(char,"_self");
                    }}
            };}};




1 个答案:

答案 0 :(得分:0)

您在声明send之前致电onreadystatechange,这就是为什么它无效:

function chkPwrd() {
  var fname = document.getElementById("fname").value;
  var lname = document.getElementById("lname").value;
  var umail = document.getElementById("umail1").value;
  var upass = document.getElementById("upass").value;
  var xhr = new XMLHttpRequest();

  if (flag === 1) {
    xhr.open("POST", "conCheck?umail=" + umail);
    alert("HIIII 1");

    xhr.onreadystatechange = function() {
      alert("HIIII 2");
      if (xhr.readyState === 4 & xhr.status === 200) {
        alert("HIIII 3");
        var a = xhr.responseText;

        if (a.indexOf('5') !== -1) {
          alert("Emailid already Exists");
          document.getElementById('umail1').style.color = "red";
          // document.getElementById('umail1').innerHTML="Emailid already Exists";
        }

        if (a.length === 0) {
          alert("Registering you..please click OK");
          var char = "register.jsp?fname=" + fname + "&lname=" + lname + "&umail=" + umail + "&upass=" + upass;
          window.open(char, "_self");
        }
      }
    };

    xhr.send(null);
  }
};