Understanding Ajax step by step (1)

Source: Internet
Author: User

Ajax is short for Asynchronous JavaScript and XML.
Purpose: Ajax allows you to use JavaScript statements to call the XMLHTTPRequest object and communicate directly with the server. You can exchange data with the server without reloading the page.

1. Create an XMLHTTPRequest object
VaR xhr = new XMLHttpRequest ()
For earlier versions of IE (IE7 and earlier versions), new activexobject ("Microsoft. XMLHTTP") and new activexobject ("msxml2.xmlhttp") are used to create objects.

2. Common attributes and common methods of XMLHttpRequest objects
Attribute
Readystate returns the current status code of the XMLHTTP request.
Return the HTTP status code of the current request.
Statustext returns the text corresponding to the HTTP status code

Method
Onreadystatechange listens to readystate and state
Open connection establishment request
Send Connection Request

Readystate status code
0 (not initialized) the object has been created, but has not been initialized
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 code 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 responsebody and responsetext to obtain the complete response data.

Common State Codes
200 OK the server returns the webpage
404 Not found client all requested pages do not exist
503 service unavailable Server Response timeout

The open (method, URL, flag, name, password) method has five parameters.
The method parameter specifies the method used to send HTTP requests to the server. The parameters can be get, post, head, put, or delete.
URL specifies the server URL
The flag parameter is used to specify the method for submitting an HTTP request. True is asynchronous (default), and false is synchronous.
Name: User Name submitted. Optional.
Password. Optional.

// Create an XMLHTTPRequest object
Function createxhr (){
Return window. XMLHttpRequest? New XMLHttpRequest (): New activexobject ("Microsoft. XMLHTTP ");
}

Function Ajax (){

VaR xhr = new createxhr ();
Xhr. onreadystatechange = function (){
If (4 = xhr. readystate & 200 = State ){
/* Succeeded */
}
Else {
/* Exception */
}
}
Xhr. Open ("get", URL, true );
Xhr. Send ();
}

 

Related Article

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.