The principle of AJAX is simply to use the XMLHTTPRequest object to send asynchronous requests to the server, obtain data from the server, and then use JavaScript to operate the Dom to update the page. The most critical step is to obtain request data from the server. To understand the process and principles, we must understand XMLHttpRequest.
XMLHttpRequest is the core mechanism of Ajax. It is first introduced in ie5. it is a technology that supports asynchronous requests. Simply put, JavaScript can promptly request and process responses to the server without blocking users. Achieve the effect of refreshing.
So let's start with XMLHttpRequest to see how it works.
First, let's take a look at the attributes of the XMLHTTPRequest object.
Its attributes include:
Onreadystatechange the event handler of the event triggered by each state change.
The string format of responsetext returned data from the server process.
Responsexml is a dom-compatible document data object returned by the server process.
Status Code returned from the server, such as common 404 (not found) and 200 (ready)
String information of the Status text accompanied by the status code
Readystate object status value
0 (not initialized) the object has been created, but has not been initialized (the open method has not been called)
1 (initialization) the object has been created and the send method has not been called
2 (send data) The send method has been called, but the current status and HTTP header are unknown.
3 (in data transmission) Some data has been received. Because the response and HTTP headers are incomplete, an error occurs when retrieving part of data through responsebody and responsetext,
4. After receiving the data, you can use responsexml and responsetext to obtain the complete response data.
However, because of the differences between browsers, creating an XMLHTTPRequest object may require different methods. This difference is mainly reflected in IE and other browsers. The following is a standard method for creating an XMLHTTPRequest object.
Function createxmlhttp ()
{
// Create an XMLHTTPRequest object in a non-IE browser
If (window. XMLHttpRequest)
{
XMLHTTP = new XMLHttpRequest ();
}
// Create an XMLHTTPRequest object in IE
If (window. activexobject)
{
Try
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (E)
{
Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
}
Catch (Ex ){}
}
}
}
Function ustbwuyi ()
{
VaR DATA = Document. getelementbyid ("username"). value;
Createxmlhttp ();
If (! XMLHTTP)
{
Alert ("An error occurred while creating the XMLHTTP object! ");
Return false;
}
XMLHTTP. Open ("Post", URL, false );
XMLHTTP. onreadystatechange = function ()
{
If (XMLHTTP. readystate = 4)
{
Document. getelementbyid ("user1"). innerhtml = "data loading ...";
If (XMLHTTP. Status = 200)
{
Document. Write (XMLHTTP. responsetext );
}
}
}
XMLHTTP. Send ();
}
As shown above, the function first checks the overall state of XMLHttpRequest and ensures that it has been completed (readystatus = 4), that is, the data has been sent. Then, query the Request status based on the server settings. If everything is ready (status = 200), perform the following operations.
For the XMLHttpRequest methods, open and send, the open method specifies:
A. Type of data submitted to the server, that is, post or get.
B. request URL and transmitted parameters.
C. Transmission Mode. If the value is false, synchronization is performed. If the value is true, asynchronous transmission is performed. The default value is true. If it is an asynchronous communication method (true), the client will not wait for the server to respond; if it is a synchronous mode (false), the client will wait until the server returns a message before executing other operations. We need to specify the synchronization mode based on actual needs. In some pages, multiple requests may be sent, or even highly-intensive requests with organized and planned structures, the other one will overwrite the previous one. In this case, you must specify the synchronization mode.
The send method is used to send requests.
After learning about the XMLHttpRequest workflow, we can see that XMLHttpRequest is completely used to send a request to the server. Its role is also limited to this, but its role is the key to the entire Ajax implementation, because Ajax is nothing more than two processes, sending requests and responding to requests. And it is completely a client technology. XMLHttpRequest is so important because it handles the communication between the server and the client.
Now we can have a general understanding of the principles of Ajax. We can regard the server as a data interface, which returns a plain text stream. Of course, this text stream can be in XML format, HTML, or JavaScript code, it can also be a string. At this time, XMLHttpRequest requests this page from the server, and the server writes the text results to the page. This is the same as the normal Web development process, after the client asynchronously obtains this result, it is not directly displayed on the page, but processed by JavaScript first and then displayed on the page. As for many popular Ajax controls such as magicajax, other data types such as dataset can be returned, but the results encapsulated by this process are essentially no big difference.