Since I learned Ajax, it has been in the fog in the cloud. I want to summarize it, but I have no way to start it. After reading DRP recently, Mr. Wang Yong briefly talked about Ajax and made another explanation of the Technical and technical things. He felt that he had understood it a little. Now I will share my understanding with you.
What Is Ajax first?
Ajax introduction:
The term Ajax was first proposed by Jesse James Garnett. The explanation at the time was Asynchronous JavaScript and XML. Although Jesse James Garnett published an article about this technology, however, when people are familiar with Google, they are still after Google Maps and Google search engines are released at Google Labs.
In fact, Ajax is a technology used to create a fast dynamic web page. It is a synthesis of several technologies, including:
Standard representation based on HTML + CSS;
Use the (DOM) Document Object Model for Dynamic Display and interaction;
Use XMLHttpRequest for asynchronous communication with the server;
Binding and processing with JavaScript;
Use XML for data exchange.
What is the core of Ajax?
Core Ajax object: XMLHTTPRequest object
The XMLHTTPRequest object is an object of JavaScript. In fact, this object came into being when ie5 (released in 1995) was launched. It is implemented as an object-ActiveX, other browsers implement it as a local JavaScript Object. It is precisely because of these differences that JavaScript contains some logic, this allows you to create an XMLHttpRequest instance by using ActiveX or local JavaScript Object Technology. How to create it?
Create an XMLHTTPRequest object:
var xmlhttp=null;if (window.XMLHttpRequest){//IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest(); }else{//IE6, IE5xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}Analysis:
First, create the global variable XMLHTTP to save XMLHttpRequest. Depending on the objects returned by different browsers, decide to instantiate the object. Firefox and other browsers call local JavaScript to instantiate the object, while ie5, IE6 creates an XMLHttpRequest instance based on the ActiveX object. What are the main methods and attributes of this object?
XMLHttpRequest methods and attributes:
Method:
Analysis:
Open (method, URL, async): creates an application to the server. There are three parameters: parameter 1: The submission method (get, post, put) is required; parameter 2: provide the URL of the resource to be called. Parameter 3: indicates whether the value is synchronous or asynchronous. The default value is true, indicating asynchronous.
Send (string): String, an optional parameter. A request is sent to the server. If the request is declared as Asynchronous, the method returns immediately. Otherwise, the system waits until the server receives a response. String, used only for post requests.
SetRequestHeader (header, value): contains two parameters. The first parameter indicates the set header, and the second parameter indicates the value to be placed in the header.
Attribute:
After learning about the creation, method, and attribute of XMLHttpRequest, how can we use ajax to implement a simple request?
Five-step usage of the XMLHTTPRequest object:
1. Create an XMLHTTPRequest object;
2. register the callback function;
3. Use the Open Method to interact with the server;
4. Set the sent data to start interacting with the server;
5. In the callback function, determine whether the interaction ends, whether the response is correct, and update the page content based on the data returned by the server.
Instance:
Front-end:
<Body class = "body1" onLoad = "Init () "> <form name =" userform "target =" _ Self "id =" userform "> <Div align =" center "> <HR width =" 97% "align =" Center "size = 0> <Table width =" 95% "border =" 0 "cellpadding =" 0 "cellspacing =" 0 "> <tr> <TD width =" 22% "Height = "29"> <Div align = "right"> <font color = "# ff0000"> * </font> User code: </div> </TD> <TD width = "78%"> <input name = "userid" type = "text" class = "text1" id = "userid" onblur = "Validate () "size =" 10 "maxlength =" 10 "> <span id =" spanuserid "> </span> </TD> </tr> <TD Height =" 26 "> <Div align =" right "> <font color =" # ff0000 "> * </font> User Name: </div> </TD> <input name = "username" type = "text" class = "text1" id = "username" size = "20" maxlength = "20"> </TD> </tr> <TD Height = "26"> <Div align = "right"> <font color = "# ff0000"> * </font> password: </div> </TD> <label> <input name = "password" type = "password" class = "text1" id = "password" size = "20 "maxlength =" 20 "> </label> </TD> </tr> </table> </div> </form> </body>
JS:
VaR XMLHTTP; function createxmlhttprequest () {// 1: Create an XMLHTTPRequest object // IE7 +, Firefox, and other if (window. XMLHttpRequest) {XMLHTTP = new XMLHttpRequest ();} else {// ie5, ie6if (window. activexobject) {XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");}}}
Function validate () {var field = Document. getelementbyid ("userid"); If (TRIM (field. Value). length! = 0) {// 1: Create the Ajax core object xmlhttprequestcreatexmlhttprequest (); var url = "user_validate.jsp? Userid = "+ trim (field. value) + "& time =" + new date (). gettime (); // 2: registers the callback function and returns the method to the onredystatechange attribute XMLHTTP. onreadystatechange = callback; // 3: Use the open method to set the request method, URL, and asynchronous XMLHTTP. open ("get", URL, true); // 4: Send the setting information to the Ajax engine XMLHTTP. send (null);} else {document. getelementbyid ("spanuserid "). innerhtml = "";}}
Function callback () {// 5: In the callback function, determine whether the interaction ends, whether the response is correct, and update the page content based on the data returned by the server. // Interaction with the server is completed, that is, if (XMLHTTP. readystate = 4) {// 200 indicates "HTTP Protocol Status successful", 404 indicates "not found", and 500 indicates "internal server error" If (XMLHTTP. status = 200) {If (TRIM (XMLHTTP. responsetext )! = "") {Document. getelementbyid ("spanuserid "). innerhtml = "<font color = 'red'>" + XMLHTTP. responsetext + "</font>";} else {document. getelementbyid ("spanuserid "). innerhtml = "" ;}} else {alert ("request failed, error code =" + XMLHTTP. status );}}}The effect is as follows:
Summary:
Ajax, in short, sends an asynchronous request to the server through the XMLHTTPRequest object, obtains data from the server, and then uses JavaScript to operate the Dom to update the page, the most critical step is to obtain request data from the server, to ensure that the page is Refresh, to communicate with the server within the page, without interrupting the user's operations, and to have a more rapid response capability.