XMLHttpRequest property responseType is an enumerated string value specifying the type of data contained in the response. It also lets the author change the response type. If an empty string is set as the value of responseType , the default value of text 被使用。

句法

var type = XMLHttpRequest.responseType;
XMLHttpRequest.responseType = type;
					

A string taken from the XMLHttpRequestResponseType enum which specifies what type of data the response contains.

When setting responseType to a particular value, the author should make sure that the server is actually sending a response compatible with that format. If the server returns data that is not compatible with the responseType that was set, the value of response 将是 null .

The values supported by responseType are the following:

""
responseType string is treated the same as "text" , the default type.
arraybuffer
response is a JavaScript ArrayBuffer containing binary data.
blob
response Blob object containing the binary data.
document
response HTML Document or XML XMLDocument , as appropriate based on the MIME type of the received data. See HTML 在 XMLHttpRequest to learn more about using XHR to fetch HTML content.
json
response is a JavaScript object created by parsing the contents of received data as JSON .
text
response is a text in a DOMString 对象。
ms-stream
response is part of a streaming download; this response type is only allowed for download requests, and is only supported by Internet Explorer.

异常

InvalidAccessError
An attempt was made to change the value of responseType XMLHttpRequest which is in synchronous mode but not in a Worker . For additional details, see Synchronous XHR restrictions 下文。

用法注意事项

Synchronous XHR restrictions

You cannot change the value of responseType in a synchronous XMLHttpRequest except when the request belongs to a Worker . This restriction is designed in part to help ensure that synchronous operations aren't used for large transactions that block the browser's main thread, thereby bogging down the user experience.

XMLHttpRequest s are asynchronous by default; they are only placed in synchronous mode by passing false as the value of the optional async parameter when calling open() .

Restrictions in Workers

Attempts to set the value of responseType to document are ignored in a Worker .

规范

规范 状态 注释
XMLHttpRequest 实时标准 WHATWG (Web 超文本应用程序技术工作组) 实时标准

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
responseType Chrome 31 Edge 12 Firefox 6 IE 10 Opera 18 Safari 7 WebView Android 55 Chrome Android 55 Firefox Android 50 Opera Android 42 Safari iOS 7 Samsung Internet Android 6.0
arraybuffer Chrome 31 Edge 12 Firefox 6 IE 10 Opera 18 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
blob Chrome 31 Edge 12 Firefox 6 IE 10 Opera 18 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
document Chrome 31 Edge 12 Firefox 11 IE 10 Opera No Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
json Chrome 31 Edge 79 Firefox 10 IE No Opera 18 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android ? Safari iOS ? Samsung Internet Android Yes
moz-blob 弃用 非标 Chrome No Edge No Firefox 12 — 58 IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
moz-chunked-arraybuffer 弃用 非标 Chrome No Edge No Firefox 14 — 68 IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 50 — 68 Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

另请参阅

元数据

  • 最后修改: