Original article: Native Ajax basic explanation and compatibility Processing
Ajax = Asynchronous JavaScript and XML (Asynchronous JavaScript and XML ).
Ajax is not a new technology, but a hot technology. It can interact with the server and update the webpage module without reload (refresh) The whole page.
Ajax has many advantages: it can be refreshed locally and loaded on demand, thus reducing the data traffic on the server. In addition, when the page is updated, other content on the web page can be browsed by the user without being affected, which also reduces the structural burden. Ajax is not omnipotent, and Seo is also affected when the above advantages exist.
Before learning Ajax, you must have the basics of HTML/XHTML, CSS, and JavaScript/Dom.
When Ajax interacts with the server, it must involve the server. At the same time, it involves the creation of the server request object (New XMLHttpRequest () and the confirmation of the Request Method (open ()) send () and responsetext ).
Creation object:
Ie9 + and other browsers support using the new XMLHttpRequest () object creation method, while IE8 and the following use the new activexobject () method.
I have seen that many people use the following code for compatibility on the Internet:
1 Try { 2 Xml = New Activexobject ("msxml2.xmlhttp" ); 3 } Catch (E ){ 4 Try { 5 Xml = New Activexobject ("Microsoft. XMLHTTP" ); 6 } Catch (E1 ){ 7 Xml = New XMLHttpRequest (); 8 } 9 }
The author uses the ie11 debugging function to test ie10 and the new activexobject ("msxml2.xmlhttp") without writing new activexobject ("msxml2.xmlhttp"). Therefore, the code can be used to create an object:
VaR xml = Window. activexobject? New activexobject ("Microsoft. XMLHTTP"): New XMLHttpRequest ();
Confirmation request:
XML. Open ('get', 'url', true/false );
The first parameter indicates the access method of string. There are two values: Get/post. Most of the time, get is used.
The second parameter indicates the URL of the server to be connected. String
The third parameter represents: Boolean. Indicates whether asynchronous requests are required (true indicates that asynchronous loading is initiated)
Send request:
XML. Send ();
If you want to send data, use XML. Send (STR );
Response Data:
XML. onreadystatechange = function (){
If (XML. readystate = 4 & xml. Status = 200 ){
Alert (XML. responsetext );
}
}
Status: returns the status of the link. Generally, 200 and 404,200 are returned, indicating that the link is successful and 404 is not found.
Readystate has five values: 0, 1, 2, 3, and 4. Onreadystatechange is triggered every time the value is changed.
The five values of readystate are described as follows:
- 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.
that is, when the request is completed and the page is found, the data on the server is obtained.