From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/xmlhttprequest/responsetext/index.html | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 files/zh-cn/web/api/xmlhttprequest/responsetext/index.html (limited to 'files/zh-cn/web/api/xmlhttprequest/responsetext') diff --git a/files/zh-cn/web/api/xmlhttprequest/responsetext/index.html b/files/zh-cn/web/api/xmlhttprequest/responsetext/index.html new file mode 100644 index 0000000000..5b6708acd4 --- /dev/null +++ b/files/zh-cn/web/api/xmlhttprequest/responsetext/index.html @@ -0,0 +1,114 @@ +--- +title: XMLHttpRequest.responseText +slug: Web/API/XMLHttpRequest/responseText +tags: + - XMLHttpRequest.responseText +translation_of: Web/API/XMLHttpRequest/responseText +--- +
{{draft}}
+ +
{{APIRef('XMLHttpRequest')}}
+ +

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

+ +

语法

+ +
var resultText = XMLHttpRequest.responseText;
+ +

取值

+ +

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

+ +

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

+ +

当请求状态{{domxref("XMLHttpRequest.readyState", "readyState")}}变为{{domxref("XMLHttpRequest.DONE", "XMLHttpRequest.DONE")}} (4),且{{domxref("XMLHttpRequest.status", "status")}}值为200("OK")时,responseText是全部后端的返回数据

+ +

例子

+ +
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);
+ +

格式

+ + + + + + + + + + + + + + +
格式状态备注
{{SpecName('XMLHttpRequest', '#the-responsetext-attribute')}}{{Spec2('XMLHttpRequest')}}WHATWG living standard
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}[1]{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

[1] 在IE10前的版本请求完成时, XMLHttpRequest.responseText 的值为只读。

-- cgit v1.2.3-54-g00ecf