Ajax Research on readystate (state value) and status (status code)

Source: Internet
Author: User
Tags response code

var getxmlhttprequest = function () {    try{        ///Mainstream browser provides XMLHttpRequest object        return new XMLHttpRequest ();    } catch (E) {        ///low version of IE browser does not provide XMLHttpRequest object, IE6 below        //So must use the specific implementation of IE browser ActiveXObject        return new ActiveXObject ("Microsoft.XMLHTTP");}    ; var xhr = Getxmlhttprequest ();//readyState 0=> initialize 1=> load 2=> load complete 3=> parse 4=> finish//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 status    //Console.log (xhr.readystate );  2 3 4    if (xhr.readystate = = 4 && Xhr.status = =) {        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

12345 0:初始化,XMLHttpRequest对象还没有完成初始化1:载入,XMLHttpRequest对象开始发送请求2:载入完成,XMLHttpRequest对象的请求发送完成3:解析,XMLHttpRequest对象开始读取服务器的响应4:完成,XMLHttpRequest对象读取服务器响应结束

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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 1xx:信息响应类,表示接收到请求并且继续处理2xx:处理成功响应类,表示动作被成功接收、理解和接受3xx:重定向响应类,为了完成指定的动作,必须接受进一步处理4xx:客户端错误,客户请求包含语法错误或者是不能正确执行5xx:服务端错误,服务器不能正确执行一个正确的请求100——客户必须继续发出请求101——客户要求服务器根据请求转换HTTP协议版本200——交易成功201——提示知道新文件的URL202——接受和处理、但处理未完成203——返回信息不确定或不完整204——请求收到,但返回信息为空205——服务器完成了请求,用户代理必须复位当前已经浏览过的文件206——服务器已经完成了部分用户的GET请求300——请求的资源可在多处得到301——删除请求数据302——在其他地址发现了请求数据303——建议客户访问其他URL或访问方式304——客户端已经执行了GET,但文件未变化305——请求的资源必须从服务器指定的地址得到306——前一版本HTTP中使用的代码,现行版本中不再使用307——申明请求的资源临时性删除400——错误请求,如语法错误401——请求授权失败402——保留有效ChargeTo头响应403——请求不允许404——没有发现文件、查询或URl405——用户在Request-Line字段定义的方法不允许406——根据用户发送的Accept拖,请求资源不可访问407——类似401,用户必须首先在代理服务器上得到授权408——客户端没有在用户指定的饿时间内完成请求409——对当前资源状态,请求不能完成410——服务器上不再有此资源且无进一步的参考地址411——服务器拒绝用户定义的Content-Length属性请求412——一个或多个请求头字段在当前请求中错误413——请求的资源大于服务器允许的大小414——请求的资源URL长于服务器允许的长度415——请求资源不支持请求项目格式416——请求中包含Range请求头字段,在当前请求资源范围内没有range指示值,请求也不包含If-Range请求头字段417——服务器不满足请求Expect头字段指定的期望值,如果是代理服务器,可能是下一级服务器不能满足请求500——服务器产生内部错误501——服务器不支持请求的函数502——服务器暂时不可用,有时是为了防止发生系统过载503——服务器过载或暂停维修504——关口过载,服务器使用另一个关口或服务来响应用户,等待时间设定值较长505——服务器不支持或拒绝支请求头中指定的HTTP版本

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

var getxmlhttprequest = function () {  if (window. XMLHttpRequest) {    return new XMLHttpRequest ();  }  else if (window. ActiveXObject) {    return new ActiveXObject ("Microsoft.XMLHTTP");}  ; var xhr = 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

var getxmlhttprequest = function () {  try{    return new XMLHttpRequest ();  } catch (e) {    return new ActiveXObject ("Microsoft.XMLHTTP");}  }; var xhr = Getxmlhttprequest (), Xhr.open ("Get", "1.txt", true); Xhr.send (); xhr.onreadystatechange = function () {    if ( Xhr.status = = =) {        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)

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.