Basic Ajax code

Source: Internet
Author: User

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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.