IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
Topic
  • 2 replies
  • Latest Post - ‏2014-09-20T12:06:44Z by jitupkp
Tanima
Tanima
1 Post

Pinned topic xmlhttp onreadystatechange error

‏2011-03-26T06:00:39Z |
Hello everyone,
I have a problem in Ajax ready state...the code i have given below is normally what we use when we have to send a request. But here when the onreadystatechange gets called it doesn't wait until readystate changes to 4 i.e completed, and alerts error. when the function ready() is called the readystate is 1 and prints error.
What to do kindly help as soon as possible..

function checkUser(FORM){
  • var xmlhttp=GetXmlHttpObject();*
  • *
  • if(xmlhttp){*
  • xmlhttp.open("POST","CheckUsername" ,true); *
  • xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');*
  • xmlhttp.send("Username="+document.getElementById("Username"));*
  • xmlhttp.onreadystatechange = ready(xmlhttp);*
  • *
* }*
  • return ;*
}

function ready(xmlhttp){
  • if(xmlhttp.readyState == 4 && xmlhttp.status==200){*
  • //alert(xmlhttp.status);*
  • var response = xmlhttp.responseText;*
  • if(response=="false"){*
  • document.getElementById('User_rep').style.fontSize="small";*
  • document.getElementById('User_rep').style.color="#990000";*
  • document.getElementById('User_rep').innerHTML="Username not available. Try another!"; *
  • } *
  • }*
  • else {*
  • alert("error"); *
  • }*
  • return ;*
  • *
}

Thankyou.
Updated on 2011-03-28T08:46:50Z at 2011-03-28T08:46:50Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: xmlhttp onreadystatechange error

    ‏2011-03-28T08:46:50Z  
    [0] This line is the main cause of error. It should read like this.
    
    xmlhttp.onreadystatechange = function() 
    {ready(xmlhttp);
    };
    

    [1] And then there are other problems. If the element of id "Username" is a form element, you should pass its value (.value) instead to be correct. Furthermore, the name/value pair should be encoded to escape "danger" characters. With these two considerations, the line should read like this.
    
    xmlhttp.send(
    "Username="+encodeURIComponent(document.getElementById(
    "Username").value));
    
  • jitupkp
    jitupkp
    104 Posts

    Re: xmlhttp onreadystatechange error

    ‏2014-09-20T12:06:44Z  

    see more

    http://techgurulab.com/course/ajax-quiz-online/