Ajax research on readyState (state value) and status (status code)

var getXmlHttpRequest = function () {
    try{
        //Mainstream browsers provide XMLHttpRequest objects
        return new XMLHttpRequest();
    }catch(e){
        //Lower version of IE browser does not provide XMLHttpRequest object, below IE6
        //So you must use the specific implementation ActiveXObject of IE browser
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
};
var xhr = getXmlHttpRequest ();
// readyState 0=>initialize 1=>load 2=>load complete 3=>parse 4=>complete
// console.log(xhr.readyState);  0
xhr.open("TYPE", "URL", true);
// console.log(xhr.readyState);  1
xhr.send();
// console.log(xhr.readyState);  1
xhr.onreadystatechange = function () {
    // console.log(xhr.status); //HTTP status?
    // console.log(xhr.readyState);  2 3 4
    if(xhr.readyState === 4 && xhr.status === 200){
        alert(xhr.responseText);
    }
};
copy code

 

1. Ajax: the difference between readyState (status value) and status (status code)
readyState refers to the steps that AJAX has experienced during running, and will respond regardless of whether the access is successful or not. It can be understood as an AJAX operation step, using " "Ajax.readyState" to get the
status means that no matter whether the AJAX access is successful or not, the HTTP protocol uses "ajax.status" to obtain the overall understanding according to the submitted information and the HTTP header information code returned by the server
: it can be simply understood as the state representative an overall state. And status is the specific small state under this big state.

2. What is readyState
readyState is an attribute of the XMLHttpRequest object, which is used to identify the current state of the XMLHttpRequest object.
readyState has a total of 5 state values, 0~4 respectively, each value represents a different meaning

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

3. What is status
status is an attribute of the XMLHttpRequest object, indicating the HTTP status code of the response.
Under the HTTP1.1 protocol, the HTTP status code can be divided into 5 categories in total

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1 xx:信息响应类,表示接收到请求并且继续处理
2 xx:处理成功响应类,表示动作被成功接收、理解和接受
3 xx:重定向响应类,为了完成指定的动作,必须接受进一步处理
4 xx:客户端错误,客户请求包含语法错误或者是不能正确执行
5 xx:服务端错误,服务器不能正确执行一个正确的请求
 
100 ——客户必须继续发出请求
101 ——客户要求服务器根据请求转换HTTP协议版本
200 ——交易成功
201 ——提示知道新文件的URL
202 ——接受和处理、但处理未完成
203 ——返回信息不确定或不完整
204 ——请求收到,但返回信息为空
205 ——服务器完成了请求,用户代理必须复位当前已经浏览过的文件
206 ——服务器已经完成了部分用户的GET请求
300 ——请求的资源可在多处得到
301 ——删除请求数据
302 ——在其他地址发现了请求数据
303 ——建议客户访问其他URL或访问方式
304 ——客户端已经执行了GET,但文件未变化
305 ——请求的资源必须从服务器指定的地址得到
306 ——前一版本HTTP中使用的代码,现行版本中不再使用
307 ——申明请求的资源临时性删除
400 ——错误请求,如语法错误
401 ——请求授权失败
402 ——保留有效ChargeTo头响应
403 ——请求不允许
404 ——没有发现文件、查询或URl
405 ——用户在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. Thinking questions: Why does the function implementation of onreadystatechange need to judge readyState and status at the same time?

First way of thinking: just use readyState

copy code
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);
  }
};
copy code

服务响应出错了,但还是返回了信息,这并不是我们想要的结果
如果返回不是200,而是404或者500,由于只使用readystate做判断,它不理会放回的结果是200、404还是500,只要响应成功返回了,就执行接下来的javascript代码,结果将造成各种不可预料的错误。所以只使用readyState判断是行不通的。

第二种思考方式:只使用status判断

copy code
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 === 200) {
        alert("readyState=" + xhr.readyState + xhr.responseText);
    }
};
copy code

In fact, the results were not as expected. The response code did return 200, but the window popped up 3 times in total! The first time is the window with "readyState=2", the second time is the window with "readyState=3", and the third time is the window with "readyState=4". From this, it can be seen that the execution of the onreadystatechange function is not triggered only when the readyState becomes 4, but every change of the readyState (2, 3, 4) will be triggered, so the situation mentioned above occurs. It can be seen that using status judgment alone will not work.

5. From the above test, we can know that readyState and status are indispensable when judging. So will the order of judging readyState and status have any effect? We can adjust the status to the front to judge first, the code such as xhr.status === 200 && xhr.readyState === 4
In fact, this has no effect on the final result, but the performance in the middle is different. From the experiment, we know that each change of readyState will trigger the onreadystatechange function. If the status is judged first, then the status of the status will be judged one more time each time. Although the performance impact is minimal, you should still put the judgment of readyState in the front with the idea of ​​pursuing the ultimate code.
xhr.readyState === 4 && xhr.status === 200

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325021547&siteId=291194637