About ajax,
Ajax Introduction
Ajax is "Asynchronous Javascript And XML" (Asynchronous JavaScript And XML). It is a Web page development technology used to create interactive web applications. It is not a new language.
By performing a small amount of data exchange with the server in the background, ajax can implement asynchronous updates on webpages.
This means that you can update a part of a webpage without reloading the entire webpage. To update the content of a traditional web page, you must reload the entire web page.
Ajax Technical System
XHTML and CSS
Use Document Object Model for Dynamic Display and Interaction
Use XML and XSLT for Data Interaction and operations
Use XMLHttpRequest for asynchronous data receiving
Ajax Workflow
XmlHttpRequest object
Attribute:
ReadyState
0: the request is not initialized.
1: The server connection has been established.
2: The request has been received
3: The request is being processed
4: The request is complete and the response is ready.
State
200: "OK"
404: Page not found
ResponseText
Obtain response data in string format.
ResponseXML
Obtain response data in XML format.
Onreadystatechange
A storage function (or function name) that is called whenever the readyState attribute changes.
Method:
The xmlHttpRequst object uses the send () and open () Methods to interact with the server.
Open (method, url, async)
Method: Request type; GET or POST
Url: the location of the file on the server
Async: true (asynchronous) or false (synchronous)
Send (string)
String: used only for POST requests.
For a post request, you must use setRequestHeader () to add an HTTP header. Then set the data to be sent in the send () method, for example:
Xmlhttp. open ("POST", "ajax_test.asp", true );
Xmlhttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
Xmlhttp. send ("fname = Bill & lname = Gates ");
How to Use ajax
1. Create an XMLHttpRequest object
Different browsers use different asynchronous call objects. in IE, asynchronous calls use the XMLHttpRequest object in the XMLHTTP component, while in Netscape and Firefox, XMLHttpRequest is used directly. Therefore, the method for creating XMLHttpRequest objects in different browsers is different.
You can create an XMLHttpRequest object in IE as follows:
Var xmlHttpRequest = new ActiveXObject ("Microsoft. XMLHTTP ");
The following describes how to create an XMLHttpRequest object in the Netscape Browser:
Var xmlHttpRequest = new XMLHttpRequest ();
2. Create an HTTP request
After creating an XMLHttpRequest object, you must create an HTTP request for the XMLHttpRequest object to describe where the XMLHttpRequest object needs to obtain data. it can usually be data in the website or in other local files.
You can use the open () method of the XMLHttpRequest object to create an HTTP request. The syntax code is as follows:
XMLHttpRequest. open (method, URL, flag, name, password)
The parameters in the Code are described as follows:
Method: this parameter is used to specify the HTTP request methods. There are five methods: get, post, head, put, and delete. The common methods are get and post.
URL: Specifies the URL of an HTTP request. It can be an absolute or relative URL.
Flag: this parameter is optional and its value is boolean. This parameter is used to specify whether asynchronous mode is used. True indicates asynchronous mode and false indicates synchronous mode. The default value is true.
Name: this parameter is optional and is used to enter the user name. This parameter is required if the server needs verification.
Password: this parameter is optional and is used to enter the password. This parameter is required if the server needs verification. You can use the following code to access the content of a website file.
3. Set the function to respond to HTTP Request status changes
After creating an HTTP request, you can send the HTTP request to the Web server. However, HTTP requests are sent to receive data returned from the server. From creating an XMLHttpRequest object, to sending data, receiving data, and XMLHttpRequest objects, they will go through the following 5 statuses.
(1) not initialized.When the XMLHttpRequest object is created, the object is in the uninitialized State. In this case, the value of the readyState attribute of the XMLHttpRequest object is 0.
(2) initialization status.When an HTTP request is created using the open () method after the XMLHttpRequest object is created, the object is initialized. The value of the readyState attribute of the XMLHttpRequest object is 1.
(3) Sending data status.After the XMLHttpRequest object is initialized, when the send () method is used to send data, the object is in the sending data state. In this case, the value of readyState of the XMLHttpRequest object is 2.
(4) Status of received data.After the Web server receives and processes the data, it sends the returned results to the client. In this case, the XMLHttpRequest object is in the receiving data state, and the value of the readyState attribute of the XMLHttpRequest object is 3.
Completed.After the XMLHttpRequest object receives data, it enters the completion state. In this case, the value of the readyState attribute of the XMLHttpRequest object is 4. At this time, the received data is stored in the memory of the client computer. You can use the responseText attribute or the responseXml attribute to obtain the data.
The data returned from the server can be obtained only after the XMLHttpRequest object completes the preceding five steps. Therefore, to obtain the data returned from the server, you must first determine the state of the XMLHttpRequest object.
The XMLHttpRequest object can respond to the readystatechange event, which is triggered when the status of the XMLHttpRequest object changes (that is, when the value of the readyState attribute changes. Therefore, you can call a function through this event and determine the value of the readyState attribute of the XMLHttpRequest object in this function. If the value of readyState is 4, The responseText attribute or responseXml attribute is used to obtain data.
4. Set the statement for obtaining the data returned by the server.
If the value of the readyState attribute of the XMLHttpRequest object is 4, it indicates that the asynchronous call process is complete. You can use the responseText attribute or responseXml attribute of the XMLHttpRequest object to obtain data.
However, when the asynchronous call process is completed, it does not mean that the asynchronous call is successful. To determine whether the asynchronous call is successful, you must also determine the status attribute value of the XMLHttpRequest object, which is only 200, it indicates that the asynchronous call is successful. Therefore, to obtain the statement that the server returns data, you must first determine whether the value of the status attribute of the XMLHttpRequest object is equal to 200.
5. Send an HTTP request
After setting the preceding steps, you can send the HTTP request to the Web server. You can use the send () method of the XMLHttpRequest object to send an HTTP request. The syntax code is as follows:
XMLHttpRequest. send (data)
Data is an optional parameter. If the requested data does not require a parameter, it can be replaced by null. The format of the data parameter is similar to that of the parameter passed in the URL.
(Note: only after the send () method is used, the value of the readyState attribute of the XMLHttpRequest object changes and the readystatechange event is triggered and the function is called)
6. Partial update
After obtaining server-side data through asynchronous Ajax calls, you can use JavaScript or DOM to locally update the data in the webpage. There are three common local update Methods:
1. Update the data of the form object;
2. Update text between tags in IE browser;
3. Partial DOM refresh