aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/xmlhttprequest/responsetext/index.html
blob: 5fc809707b1e0b6763f010fa1c6d2a7a2046412e (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
---
title: XMLHttpRequest.responseText
slug: Web/API/XMLHttpRequest/responseText
tags:
  - XMLHttpRequest.responseText
translation_of: Web/API/XMLHttpRequest/responseText
---
<div>{{draft}}</div>

<div>{{APIRef('XMLHttpRequest')}}</div>

<p><strong>XMLHttpRequest.responseText </strong>在一个请求被发送后,从服务器端返回文本。</p>

<h2 id="语法">语法</h2>

<pre>var resultText = XMLHttpRequest.responseText;</pre>

<h3 id="取值">取值</h3>

<p>{{domxref("DOMString")}}<code>XMLHttpRequest</code> 返回的纯文本的值。当{{domxref("DOMString")}}<code>null</code>时,表示请求失败了。当{{domxref("DOMString")}} 为""时,表示这个请求还没有被{{domxref("XMLHttpRequest.send", "send()")}}</p>

<p>当处理一个异步request的时候,尽管当前请求并没有结束,<code>responseText</code>的返回值是当前从后端收到的内容。</p>

<p>当请求状态{{domxref("XMLHttpRequest.readyState", "readyState")}}变为{{domxref("XMLHttpRequest.DONE", "XMLHttpRequest.DONE")}} (<code>4</code>),且{{domxref("XMLHttpRequest.status", "status")}}值为200(<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">"OK"</span></font>)时,<code>responseText</code>是全部后端的返回数据</p>

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

<pre class="brush: js">var xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);

// If specified, responseType must be empty string or "text"
xhr.responseType = 'text';

xhr.onload = function () {
    if (xhr.readyState === xhr.DONE) {
        if (xhr.status === 200) {
            console.log(xhr.response);
            console.log(xhr.responseText);
        }
    }
};

xhr.send(null);</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-responsetext-attribute')}}</td>
   <td>{{Spec2('XMLHttpRequest')}}</td>
   <td>WHATWG living standard</td>
  </tr>
 </tbody>
</table>

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

{{Compat("api.XMLHttpRequest.responseText")}}