Introduction to AJAX concepts and AJAX concepts
AJAX Learning
1. Create an XMLHttpRequest object
Var request = new XMLHttpRequest ();
Compatible with ie6 and ie5
Var request;
If (exactly E. XMLHttpRequest ){
Request = new XMLHttpRequest (); // IE7 +, Firefor, Chorme, Opera ,....
} Else {
Request = new ActiveXObject ("Microsoft. XMLHTTP"); // IE6, IE5
}
2. HTTP request steps
(1) Establish a TCP connection
(2) The Web browser sends request commands to the Web server
(3) The Web server sends the request header information
(4) Web Server Response
(5) The Web server sends Response Information
(6) The Web server sends data to the browser
(7) disable TCP connection on the Web Server
3. HTTP requests are generally composed of four parts
(1) HTTP request methods or actions, such as GET or POST requests
(2) The request URL must know the request address.
(3) request headers, including client environment information and authentication information
(4) The request body is the request body. The request body can contain the query string information and form information submitted by the customer.
. GET: used to obtain information. URL-based parameters are used to transmit a limited number of sent information, which is generally 2000 characters long.
. POST: used to modify resources on the server. The number of sent messages is unlimited. Is safer than GET.
4. HTTP response is generally composed of three parts
(1) A status code consisting of an array and text to show whether the request is successful or failed
(2) The response header contains many useful information, such as the server type, date and time, content type, and length.
(3) The response body is the response body.
5. HTTP status code
. 1XX: indicates that the Web browser request is received and is being processed.
. 2XX: Successful, indicating that the user request is correctly received and processed, such as: 200 OK
. 3XX: Redirection, indicating that the request failed and the customer must take further actions
. 4XX: client error, indicating an error occurred in the request submitted by the client, for example, 404 NOT Found, indicating that the document referenced in the request does NOT exist
. 5XX: server error, indicating that the server cannot process the request: for example, 500
6. XMLHttpRequest sends a request
. Open (method, url, async) [Request method, request address, request synchronization, asynchronous]
. Send (string)
Note: The value of send can be null when a GET request is used, and the value of send in a POST request cannot be null.
Example:
Request. open ("GET", "get. php", true );
Request. send ();
Request. open ("POST", "post. php", true );
Request. send ();
Request. open ("POST", "sreate. php", "true ");
Request. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
Request. send ("name = hyp & sex = female ");
Note: the setRequestHeader () method must be used to set the http header information between open and send.
7. XMLHttpRequest gets a response
. ResponseText: Get Response Data in string format
. ResponseXML: Get Response Data in XML format
. Status and statusText: return the HTTP status code in numbers and text format
. GetAllResponseHeader (): obtains all response headers.
. GetResponseHeader (): query the value of a field in the response
. ReadyState attribute
> 0: initialization at the end of the request. open has not been called.
> 1: The server connection has been established and open has been called.
> 2: The request has been received, that is, the received header information.
> 3: The request is being processed, that is, the response body is received.
> 4: The request is complete and the response is ready, that is, the response is complete.