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/responsexml/index.html | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 files/zh-cn/web/api/xmlhttprequest/responsexml/index.html (limited to 'files/zh-cn/web/api/xmlhttprequest/responsexml/index.html') diff --git a/files/zh-cn/web/api/xmlhttprequest/responsexml/index.html b/files/zh-cn/web/api/xmlhttprequest/responsexml/index.html new file mode 100644 index 0000000000..ffd22d7896 --- /dev/null +++ b/files/zh-cn/web/api/xmlhttprequest/responsexml/index.html @@ -0,0 +1,135 @@ +--- +title: XMLHttpRequest.responseXML +slug: Web/API/XMLHttpRequest/responseXML +tags: + - XMLHttpRequest.responseXML +translation_of: Web/API/XMLHttpRequest/responseXML +--- +

{{APIRef('XMLHttpRequest')}}

+ +

XMLHttpRequest.responseXML 属性是一个只读值,它返回一个包含请求检索的HTML或XML的{{domxref("Document")}},如果请求未成功,尚未发送,或者检索的数据无法正确解析为 XML 或 HTML,则为 null。默认是当作“text / xml” 来解析。当 {{domxref("XMLHttpRequest.responseType", "responseType")}} 设置为 “document” 并且请求已异步执行时,响应将被当作 “text / html” 来解析。responseXML 对于任何其他类型的数据以及 data: URLs 为 null。

+ +
+

responseXML 在这个属性的历史堪称神器,它可以同时在 HTML 和 XML 中工作

+
+ +

如果服务器没有明确指出 {{HTTPHeader("Content-Type")}} 头是 "text/xml" 还是 "application/xml", 你可以使用{{domxref("XMLHttpRequest.overrideMimeType()")}} 强制 XMLHttpRequest 解析为 XML.

+ +

语法

+ +
var data = XMLHttpRequest.responseXML;
+
+ +

+ +

 {{domxref("Document")}} 中包含从 {{domxref("XMLHttpRequest")}} 中收到的 HTML 节点或解析后的 XML 节点,也可能是在没有收到任何数据或数据类型错误的情况下返回的 null.

+ +

例外

+ +
+
InvalidStateError
+
{{domxref("XMLHttpRequest.responseType", "responseType")}} 既不是 "document" 也不是空字符串 (接收的数据应是XML 或 HTML).
+
+ +

示例

+ +
var xhr = new XMLHttpRequest();
+xhr.open('GET', '/server', true);
+
+// 如果已指明,responseType 必须是空字符串或 "document"
+xhr.responseType = 'document';
+
+// overrideMimeType() 用来强制解析 response 为 XML
+xhr.overrideMimeType('text/xml');
+
+xhr.onload = function () {
+  if (xhr.readyState === xhr.DONE) {
+    if (xhr.status === 200) {
+      console.log(xhr.response);
+      console.log(xhr.responseXML);
+    }
+  }
+};
+
+xhr.send(null);
+ +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{SpecName('XMLHttpRequest', '#the-responsexml-attribute')}}{{Spec2('XMLHttpRequest')}}WHATWG living standard
+ +

浏览器兼容性

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

[1] 在 Firefox 51 之前, 解析收到数据的错误会在 {{domxref("Document")}} 的顶部添加一个 <parsererror> 节点,并且在任何状态下返回 Document 。这是不符合规范的。从 Firefox 51开始,这种情况可以正确的返回 null。

+ +

了解更多

+ + -- cgit v1.2.3-54-g00ecf