XMLHttpRequest.responseType

翻译不完整。 请帮助我们翻译这篇文章!

XMLHttpRequest.responseType 属性是一个枚举类型的属性,返回响应数据的类型。它允许我们手动的设置返回数据的类型。如果我们将它设置为一个空字符串,它将使用默认的"text"类型。

在工作环境(Work Environment)中将responseType的值设置为"document"通常会被忽略. 当将responseType设置为一个特定的类型时,你需要确保服务器所返回的类型和你所设置的返回值类型是兼容的。那么如果两者类型不兼容呢?恭喜你,你会发现服务器返回的数据变成了null,即使服务器返回了数据。还有一个要注意的是,给一个同步请求设置responseType会抛出一个InvalidAccessError 的异常。

responseType支持以下几种值:

""
responseType 为空字符串时,采用默认类型 DOMString,与设置为 text 相同。
arraybuffer
response 是一个包含二进制数据的 JavaScript ArrayBuffer
blob
response 是一个包含二进制数据的 Blob 对象 。
document
response 是一个 HTML DocumentXML XMLDocument,这取决于接收到的数据的 MIME 类型。请参阅 XMLHttpRequest 中的 HTML 以了解使用 XHR 获取 HTML 内容的更多信息。
json
response 是一个 JavaScript 对象。这个对象是通过将接收到的数据类型视为 JSON 解析得到的。
text
response 是一个以 DOMString 对象表示的文本。
ms-stream
response 是下载流的一部分;此响应类型仅允许下载请求,并且仅受 Internet Explorer 支持。

规范

规范 状态 注释
XMLHttpRequest Living Standard WHATWG living standard

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
responseTypeChrome Full support 31Edge Full support 12Firefox Full support 6IE Full support 10Opera No support 12 — 15
No support 12 — 15
Full support 18
Safari Full support 7WebView Android Full support 55Chrome Android Full support 55Firefox Android Full support 50Opera Android Full support 42Safari iOS Full support 7Samsung Internet Android Full support 6.0
arraybufferChrome Full support 31Edge Full support 12Firefox Full support 6IE Full support 10Opera No support 12 — 15
No support 12 — 15
Full support 18
Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 50Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
blobChrome Full support 31Edge Full support 12Firefox Full support 6IE Full support 10Opera Full support 18Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 50Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
documentChrome Full support 31Edge Full support 12Firefox Full support 11IE Full support 10Opera No support NoSafari Full support 7WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 50Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
jsonChrome Full support 31Edge Full support 79Firefox Full support 10IE No support NoOpera No support 12 — 15
No support 12 — 15
Full support 18
Safari Full support 7WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 50Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
moz-blob
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 12 — 58IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
moz-chunked-arraybuffer
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 14 — 68IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android No support 50 — 68Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

了解更多