Ajax learning, ajax

Source: Internet
Author: User

Ajax learning, ajax

1. Concept

Ajax is short for Asynchronous JavaScript and XML, that is, Asynchronous JavaScript and XML.

In traditional WEB applications, each user operation in a page triggers an HTTP request returned to the Web server. After the server processes the request, it returns the corresponding HEML page. In Ajax applications

The engine communicates with the server, and then submits the returned results to the client Ajax engine. Then, the Ajax engine decides to insert the data to the specified location on the page.

Therefore, the biggest feature of Ajax is that it does not need to update pages, which reduces the burden on servers.

 

 

2. Ajax technology

Ajax is a combination of JavaScript, XML, CSS, DOM, and other existing technologies.

 

2.1 XMLHttpRequest

The core technology of Ajax is XMLHttpRequest, which is a JavaScript Object with an application interface. Through it, You can exchange data only with the server, instead of refreshing the page every time.

 

Common methods and attributes of XMLHttpRequest objects are as follows:

 

 

2.2 JavaScript

Add an explanatory language for dynamic script code to the Web page.

 

2.3DOM

Document Object Model (Document Object Model) is an application interface that represents various elements (such as HTML tags and text strings) of documents and access and operations. In Ajax applications, you can use JavaScript to operate DOM to modify the user interface in real time without refreshing new pages.

 

2.4XML

Extensible Markup Language (Extensible Markup Language) provides a format for describing structured data.

 

2.5CSS

Cascading Style Sheets are used for page layout. By changing the CSS attributes of a document object, you can control the appearance and behavior of the page.

 

 

3. Traditional Ajax Workflow

 

3.1 send request

1) initialize the XMLHttpRequest object

Initialization is required before XMLHttpRequest is used. The initialization method is as follows:

 

If (window. XMLHttpRequest) {http_request = new XMLHttpRequest (); // non-IE browser} else if (window. activeObject) {// IE browser try {http_request = new ActiveObject ("Msxml2.XMLHTTP");} catch (e) {try {http_request = new ActiveObject ("Microsoft. XMLHTTP ") ;}catch (e ){}}}

 

2) specify a callback function for the XMLHttpRequest object to process the returned results.

Http_request.onreadystatechange = getResult; // call the callback function.

If you want to specify the passed parameter, you can apply the following method:

http_request.onreadystatechange=function(){getResult(param)};

3) create a connection with the server. When creating a connection, you must specify the method for sending the request and set whether to send the request asynchronously.

http_request.open("GET", url, true);// http_request.open("POST", url,true);

4) send a request to the server. The send () method can be used to send requests to the server. This method requires passing a parameter. If a GET request is sent, the parameter can be set to null. If a POST request is sent, this method uses this parameter to specify the request parameters to be sent.

Send a GET request:

http_request.send(null);

Send POST request:

Http_request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
Var param = "user =" + ......; // set parameters
Http_request.send (param );

3.2 Process Server Response

In the specified callback function, you must first determine the server's Request status to ensure that the request has been completed, and then determine whether the server's response to the request is successful Based on the server's HTTP status code, if the response succeeds, the response from the server is returned to the client. The XMLHttpRequest object provides two attributes used to access the server response: one is the responseText attribute, which returns the string response; the other is the responseXML attribute, which returns the XML response.

1) process string response

Function getResult () {if (http_request.readyState = 4) {// judge the Request status if (http_request.status = 200) {// The request is successful and starts to process the corresponding alert (http_request.responseText ); // The response result is displayed in the pop-up dialog box.} else {alert ("error ");}}}

2) process XML response

var xmldoc=http_request.responseXML;

Parse xmldoc

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.