Ajax. js
// Declare the XMLHttpRequest object
Var xmlHttp;
// This method can be copied and used
// Create an XMLHttpRequest object
Funciton createXMLHttp ()
{
If (window. XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest (); // mozilla Browser
}
Else if (window. ActiveXObject)
{
Try
{
XmlHttp = new ActiveXObject ("Msxml2.XMLHttp"); // old IE version
} Catch (e ){}
Try
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHttp"); // new IE version
} Catch (e ){}
}
If (! XmlHttp)
{
Window. alert ("Sorry! Unable to create XMLHttpRequest object ");
Return false;
}
}
// Execute the check user's callback function
// After the data is sent to the server, the change function will be used to check whether
// No message sent successfully
Function checkUserName ()
{
If (xmlHttp, readyState = 4) // judge the object status
{
If (xmlHttp. status = 200) // The message is returned successfully and the processing starts.
{
// Obtain the return value sent from the server and change some elements of the page as needed
// ResponseText is suitable for a small amount of information and responseXML is suitable for a large amount of information.
If (xmlHttp. responseText = "true") // corresponds to Response. Write ("true") in doRequest. aspx ");
{
Document. getElementById ("imageName"). src = "image/true.gif ";
// Change the image file on the page
}
Else
{
Document. getElementById ("imageName"). src = "image/false.gif ";
}
}
}
}
// Check whether the user name exists
// Onkeyup = "CheckName (document. getElementById ('username '))"
Function CheckName (userName)
{
CreateXMLHttp (); // create an XMLHttpRequest object
Var url = "doRequest. aspx? Name = "+ userName; // There is no difference in the Previous Page Submission
XmlHttp. open ("GET", url, true); // This function takes no time to submit new data.
XmlHttp. onreadystatechange = checkUserName; // callback function in the send Method
// Executed only after data is submitted
XmlHttp. send (null); // submit data after completing the preceding steps
}
DoRequest. aspx // the page for processing requests in the background. In java, it can be jspservlet.
// Obtain the sent Parameters
String userName = Request. QueryString ["name"]. Tostring ();
/** // Determine the userName to see if it is in the database
// If yes, return "true" data to the ajax page
Response. Write ("true ");
// Otherwise
Response. Write ("false ");