Ajax Learning
1.XMLHttpRequest Object creation
var request= new XMLHttpRequest ();
Compatible with IE6,IE5
var request;
if (Windoe. XMLHttpRequest) {
request= new XMLHttpRequest ();//ie7+,firefor,chorme,opera,....
}else{
Request=new ActiveXObject ("Microsoft.XMLHTTP");//ie6,ie5
}
2.HTTP Request Steps
(1) Establishing a TCP connection
(2) Web browser sends request command to Web server
(3) The Web server sends the request header information
(4) Web server answer
(5) The Web server sends the reply message
(6) The Web server sends data to the browser
(7) The Web server shuts down the TCP connection
3.HTTP requests are generally made up of four parts
(1) The method or action of the HTTP request, such as a GET or POST request
(2) The URL being requested, always know what the requested address is
(3) Request header, including some client environment information, authentication information, etc.
(4) The request body is the request text, the request body can include the query string information submitted by the Customer, form information and so on
. Get: Generally used for information acquisition, using URL to pass parameters, the number of messages sent is limited, generally in 2000 characters
. POST: Typically used to modify resources on the server. The number of messages sent is unlimited. is safer than get.
The 4.HTTP response is generally made up of three parts
(1) A status code consisting of an array and text to indicate whether the request succeeded or failed
(2) Response header, contains a lot of useful information, such as: Server type, date time, content type and length, etc.
(3) The response body is the response body
5.HTTP Status Code
.1XX: Information class, indicating receipt of Web browser request, being further processed
.2XX: Successful, indicates that the user request is received correctly, processing, such as:
.3XX: Redirect to indicate that the request was unsuccessful and the customer must take further action
.4XX: Client error, indicating that the client submitted a request with an error, such as: 404 Not Found, means that the document referenced in the request does not exist
.5XX: Server error, indicating that the server could not complete processing of the request: 500
6.XMLHttpRequest Send Request
. open (Method,url,async) [Request method, request address, request synchronous, asynchronous]
. Send (String)
Note: Send can be null when using a GET request, and send with 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 sets the HTTP header information, which must be between open and send.
7.XMLHttpRequest getting a response
. ResponseText: Get response data in string form
. Responsexml: Getting response data in XML form
. Status and StatusText: Return HTTP status codes as numbers and text
. Getallresponseheader (): Gets all the response headers
. getResponseHeader (): Query the value of a field in a response
. readystate Properties
>0: End of request initialization, Open has not been called
>1: The server connection has been established and open has been called
>2: Request received, that is, receive header information
>3: In request processing, that is, the response body is received
>4: The request is complete and the response is ready, that is, the response is complete
Introduction to the concept of Ajax