varGetxmlhttprequest =function () { Try{ //XMLHttpRequest objects are available in mainstream browsers return NewXMLHttpRequest (); }Catch(e) {//The lower version of IE does not provide XMLHttpRequest objects, IE6 the following //so you must use the specific implementation of IE browser ActiveXObject return NewActiveXObject ("Microsoft.XMLHTTP"); }};varXHR =getxmlhttprequest ();//readyState 0=> initialization 1=> loading 2=> loading complete 3=> parsing 4=> complete//Console.log (xhr.readystate); 0Xhr.open ("TYPE", "URL",true);//Console.log (xhr.readystate); 1xhr.send ();//Console.log (xhr.readystate); 1Xhr.onreadystatechange =function () { //Console.log (xhr.status);//http state? //Console.log (xhr.readystate); 2 3 4 if(Xhr.readystate = = = 4 && xhr.status = = 200) {alert (xhr.responsetext); }};
1.ajax:readystate (status value) and status (status code) differences
ReadyState, refers to the various states that run Ajax, regardless of the success of Access will respond to the steps, can be understood as an Ajax run step, using "ajax.readystate" to get
Status refers to the HTTP header information code returned by the HTTP protocol based on the information submitted, regardless of whether or not Ajax access was successful, using "Ajax.status" to obtain
General understanding: It is simple to understand that State represents a whole. The status is the small state below the big one.
2. What is readystate
ReadyState is a property of the XMLHttpRequest object that identifies what state the current XMLHttpRequest object is in.
ReadyState a total of 5 status values, respectively, 0~4, each value represents a different meaning
0: Initialize, XMLHttpRequest object has not completed initialization 1: Load, XMLHttpRequest object start sending request 2: Load complete, XMLHttpRequest Object request send complete 3: Parse, XMLHttpRequest Object starts reading server response 4: Finish, XMLHttpRequest object Read server response end
3. What is status
Status is a property of the XMLHttpRequest object that represents the HTTP status code for the response
Under the HTTP1.1 protocol, HTTP status codes can be divided into 5 major categories
1XX: Information response class, which represents receiving a request and continues processing 2xx: Processing a successful response class, indicating that the action was successfully received, understood, and accepted 3xx: Redirect Response class, in order to complete the specified action, must accept further processing 4xx: Client error, The client request contains a syntax error or is not properly executed 5xx: The server is not performing a correct request correctly 100--the customer must continue to make a request 101--the client requests that the server convert the HTTP protocol version 200--the transaction succeeds 201-- The prompt knows that the url202--of the new file is accepted and processed, but the processing is not completed 203--The return information is indeterminate or incomplete 204--the request is received, but the return information is empty 205--the server has completed the request and the user agent must reset the currently browsed file 206-- The server has completed a partial user get request 300--The requested resource can get 301--delete request data in multiple locations 302--found the request data at other addresses 303--advise the customer to access other URLs or access methods 304--the client has performed a get, but the file has not changed 305- -The requested resource must get the code that was used in the previous version of HTTP from the address specified by the server, and the current version no longer uses the 307--claim resource to temporarily delete 400--error requests, such as syntax errors 401--request authorization failed 402--306-- Retaining a valid Chargeto header response 403--request does not allow 404--to find a file, query, or url405--the user defined in the Request-line field does not allow 406--to request a resource that is not accessible based on the accept drag sent by the user 407-- Like 401, the user must first be authorized on the proxy server 408--the client does not complete the request within the user-specified hungry time 409--to the current resource state, the request cannot be completed 410--the server no longer has this resource and no further reference address 411-- The server rejects the user-defined Content-length property request 412--One or more request header fields in the current request error 413--the requested resource is greater than the server allowed size 414--The requested resource URL is longer than the length allowed by the server 415-- The request resource does not support the request Item format 416--request contains a range request header field that does not have a range indication value within the current request resource scope, and the request does not contain the If-range request header field 417--The server does not meet the expected value specified by the request Expect header field. If it is a proxy server, the next level of server may not meet the request 500--server generates an internal error 501--the server does not support the requested function 502--the server is temporarily unavailable, sometimes to prevent system overload 503--server overload or pause maintenance 504--The gateway is overloaded, the server responds to the user with another gateway or service, and the wait time setting value is longer 505--the server does not support or deny HTTP versions specified in the request header
4. Consider the question: why should the onreadystatechange function be judged readystate and status at the same time?
First way of thinking: using only readystate
varGetxmlhttprequest =function () { if(window. XMLHttpRequest) {return NewXMLHttpRequest (); } Else if(window. ActiveXObject) {return NewActiveXObject ("Microsoft.XMLHTTP"); }};varXHR =getxmlhttprequest (); Xhr.open ("Get", "1.txt",true); Xhr.send (); Xhr.onreadystatechange=function () { if(Xhr.readystate = = 4) {alert (xhr.responsetext); }};
The service responded with an error, but returned the information, which is not the result we want.
If the return is not 200, but 404 or 500, because only using readystate to judge, it ignores the result of put back is 200, 404 or 500, as long as the response successfully returned, the next JavaScript code, the result will be a variety of unpredictable errors. So only using readystate to judge is not feasible.
Second way of thinking: use status only to judge
varGetxmlhttprequest =function () { Try{ return NewXMLHttpRequest (); }Catch(e) {return NewActiveXObject ("Microsoft.XMLHTTP"); }};varXHR =getxmlhttprequest (); Xhr.open ("Get", "1.txt",true); Xhr.send (); Xhr.onreadystatechange=function () { if(Xhr.status = = 200) {alert ("Readystate=" + xhr.readystate +xhr.responsetext); }};
In fact, the results were not as expected. The response code did return 200, but it popped a total of 3 windows! The first is "readystate=2" window, the second is "readystate=3" window, the third time is "readystate=4" window. Thus, it can be seen that the execution of the onreadystatechange function is not only triggered when the readystate becomes 4, but that each change of readystate (2, 3, 4) is triggered, so there is the situation that was said earlier. As you can see, using status alone is not a viable decision.
5. By the above experiment, we can know when to judge readystate and status indispensable. Will the order of readystate and status be affected? We can put the status before the first judge, code such as Xhr.status = = = && Xhr.readystate = = = 4
In fact, this has no effect on the final result, but the performance in the middle is different. We know from the experiment that every change of readystate will trigger the onreadystatechange function, if the status is judged first, then each time it will judge the state of one more. Although the performance of the impact is very little, but still should hold the idea of pursuing extreme code, put readystate judgment in front.
Xhr.readystate = = = 4 && xhr.status = = 200
Ajax Research on readystate (state value) and status (status code)