First, create an xmlhttprequestxmlhttprequest
Function createrequest ()
{
Try
{
Request = new XMLHttpRequest ();
}
Catch (trymicrosoft)
{
Try
{
Request = new activexobject ("msxml2.xmlhttp ");
}
Catch (othermicrosoft)
{
Try
{
Request = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (failed)
{
Request = NULL;
}
}
}
If (request = NULL)
{
Alert ("error, creating request object! ");
}
}
Then write the method to send the asynchronous request:
Sendasynchronousrequest
Function ajaxlogin ()
{
VaR emaillogin = Document. getelementbyid ("useremailinput"). value;
VaR passwordlogin = Document. getelementbyid ("userpwdinput"). value;
Createrequest ();
// Add a random time character to new date (). gettime () to avoid request caching
VaR url = "ajaxlogin. aspx? Email = "+ emaillogin +" & thistime = "+ new date (). gettime ();
Request. Open ("get", URL, true );
Request. onreadystatechange = updatepageuserlogin; // The callback function updatepageuserlogin method name cannot be followed by parentheses ()
Request. Send (null );
}
Callback Function execution:
Call-back function
Function updatepageuserlogin ()
{
If (request. readystate = 4)
{
If (request. Status = 200)
{
VaR content = request. responsetext;
If (content = "true ")
{
// Alert ("Logon successful ");
Setuserloginokcookie (); // The cookie is saved to the user after the user logs on successfully.
}
If (content = "false ")
{
// Alert ("failed ");
VaR html = '<Div style = "color:"> </img> the user name or password is incorrect. Please enter it again: </div> ';
Jquery ("# loginnameandpassworderror" ).html (HTML );
}
}
}
}
In this way, Ajax asynchronous requests are completed elegantly and concisely!
Good! Try to make money to buy an iPhone for my wife $ 4600rmb