Also talk about Ajax technology (Asynchronous JavaScript and XML)

Source: Internet
Author: User

The popular Ajax is not a new technology. It is just a combination of old technologies. You can see from its full name that Ajax is Asynchronous JavaScript and XML (and DHTML) that is, Asynchronous JavaScript and XML.

To understand how Ajax works, you must master the following technologies:

(1) HTML is used to create a Web form and determine the fields used in other parts of the application.
(2) JavaScript code is the core code used to run Ajax applications and helps improve communication with server applications.
(3) DHTML or Dynamic HTML, used to dynamically update forms. We will use div, span, and other dynamic HTML elements to mark HTML.
(4) Document Object Model DOM is used to process HTML structures (using JavaScript code) and (in some cases) the XML returned by the server.

1. The most important JavaScript Object to be used in Ajax is the XMLHttpRequest object, which is the most basic object and the most important object for coding using Ajax technology, different browsers use different methods to create this object. Therefore, when developing a webpage, you must create an XMLHttpRequest object that can run on a general-purpose browser. The following Code creates this object:

<Script language = "javascript" type = "text/javascript">
Var request = false;
Try {
Request = new XMLHttpRequest ();
} Catch (trymicrosoft ){
Try {
Request = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (othermicrosoft ){
Try {
Request = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (failed ){
Request = false;
}
}
}
If (! Request)
Alert ("Error initializing XMLHttpRequest! ");
</Script>

2. After creating an object, you need to open the request. Generally, the get method is used.

Var phone = document. getElementById ("phone"). value;
Var url = "/cgi-local/lookupCustomer. php? Phone = "+ escape (phone );
Request. open ("GET", url, true );

3. Specify the callback method.

Request. onreadystatechange = updatePage;

4. Send a request

Request. send (null );

5. Callback method to process server response

Function updatePage (){
If (request. readyState = 4 ){
If (request. status = 200 ){
Var response = request. responseText. split ("| ");
Document. getElementById ("order"). value = response [0];
Document. getElementById ("address"). innerHTML = response [1]. replace (/\ n/g ,"");
} Else
Alert ("status is" + request. status );
}
}

Finally, summarize the JavaScript code written above.

<Script language = "javascript" type = "text/javascript">
Var request = false;
Try {
Request = new XMLHttpRequest ();
} Catch (trymicrosoft ){
Try {
Request = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (othermicrosoft ){
Try {
Request = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (failed ){
Request = false;
}
}
}
If (! Request)
Alert ("Error initializing XMLHttpRequest! ");

Function getCustomerInfo (){
Var phone = document. getElementById ("phone"). value;
Var url = "/cgi-local/lookupCustomer. php? Phone = "+ escape (phone );
Request. open ("GET", url, true );
Request. onreadystatechange = updatePage;
Request. send (null );
}

Function updatePage (){
If (request. readyState = 4 ){
If (request. status = 200 ){
Var response = request. responseText. split ("| ");
Document. getElementById ("order"). value = response [0];
Document. getElementById ("address"). innerHTML = response [1]. replace (/\ n/g ,"");
} Else
Alert ("status is" + request. status );
}
}
</Script>

According to the preceding five steps, local refreshing of ajax asynchronous transmission can be implemented, which is very simple.

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.