Ajax Technical Principles

Source: Internet
Author: User

Ajax is widely known to mean Asynchronous JavaScript and XML (Asynchronous JavaScript and XML ). The core of this technology is to send an asynchronous HTTP request through a browser to call the webpage or service of the server.

To use Ajax technology, you must create an XMLHTTPRequest object. Internet Explorer implements XMLHttpRequest as an ActiveX object. other browsers (such as Firefox, Safari, and opera) Implement XMLHttpRequest as a local JavaScript Object, so that they can be used in different browsers, the XMLHTTPRequest object must be created as follows.

Define an XMLHTTPRequest object
<Script language = "JavaScript" type = "text/JavaScript">
VaR XMLHTTP;
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}

</SCRIPT>

After the definition, you can send a request to the server.

Send a request to the server
Function query (URL)
{
XMLHTTP. Open ("get", URL); the first parameter is that the request method is usually get or post, and the second parameter is the URL address sent
XMLHTTP. onreadystatechange = updatedetails; // the event to be triggered after completion
XMLHTTP. Send (null );

}

Instantiate the updatedetails () function to obtain the data returned by the server
Function updatedetails ()
{

Judge different conditions by judging the XMLHTTP. readystate attribute

Readystate has five possible values:
0 (not initialized): (XMLHttpRequest) the object has been created, but the open () method has not been called.
1 (load): the open () method has been called, but no request has been sent.
2 (Loading completed): The request has been sent completely.
3 (interaction): Some response data can be received.
4 (complete): All data has been received and the connection has been closed.
}

The following is a diagram of Ajax interaction downloaded from the Internet.

Related Article

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.