aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/xmlhttprequest/statustext/index.html
blob: 7935aab46b56b0cfe22e6aec701927d856e5450d (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: XMLHttpRequest.statusText
slug: Web/API/XMLHttpRequest/statusText
translation_of: Web/API/XMLHttpRequest/statusText
---
<div>{{APIRef('XMLHttpRequest')}}</div>

<div>只读属性 <code><strong>XMLHttpRequest.statusText</strong></code> 返回了<code>XMLHttpRequest</code> 请求中由服务器返回的一个<a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMString" title="DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String."><code>DOMString</code></a> 类型的文本信息,这则信息中也包含了响应的数字状态码。不同于使用一个数字来指示的状态码<code><a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHTTPRequest/status" title="The XMLHttpRequest.status property returns an unsigned short with the status of the response of the request. These are the standard HTTP status codes. For example, status is 200 denotes a successful request. Before the request is complete, the value of status will be 0.">XMLHTTPRequest.status</a></code>,这个属性包含了返回状态对应的文本信息,例如"OK"或是"Not Found"。如果请求的状态<code><a href="/en-US/docs/Web/API/XMLHttpRequest/readyState">readyState</a></code>的值为"UNSENT"或者"OPENED",则这个属性的值将会是一个空字符串。</div>

<div></div>

<div>如果服务器未明确指定一个状态文本信息,则<code>statusText</code>的值将会被自动赋值为"OK"。</div>

<h2 id="例子">例子</h2>

<pre class="brush: js">var xhr = new XMLHttpRequest();
console.log('0 UNSENT', xhr.statusText);

xhr.open('GET', '/server', true);
console.log('1 OPENED', xhr.statusText);

xhr.onprogress = function () {
  console.log('3 LOADING', xhr.statusText);
};

xhr.onload = function () {
  console.log('4 DONE', xhr.statusText);
};

xhr.send(null);

/**
 * 输出如下:
 *
 * 0 UNSENT
 * 1 OPENED
 * 3 LOADING OK
 * 4 DONE OK
 */
</pre>

<h2 id="标准">标准</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">标准</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('XMLHttpRequest', '#the-statustext-attribute')}}</td>
   <td>{{Spec2('XMLHttpRequest')}}</td>
   <td>WHATWG living standard</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div class="hidden">此页面的兼容性表格由结构化的数据生成,如果您想贡献数据,请参见<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并向我们提出 pull request.</div>

<p>{{Compat("api.XMLHttpRequest.statusText")}}</p>

<h2 id="参考内容">参考内容</h2>

<ul>
 <li>List of <a href="/en-US/docs/Web/HTTP/Response_codes">HTTP response codes</a></li>
 <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
</ul>