XMLHttpRequest
response
property returns the response's body content as an
ArrayBuffer
,
Blob
,
Document
, JavaScript
对象
,或
DOMString
, depending on the value of the request's
responseType
特性。
var body = XMLHttpRequest.response;
An appropriate object based on the value of
responseType
. You may attempt to request the data be provided in a specific format by setting the value of
responseType
after calling
open()
to initialize the request but before calling
send()
to send the request to the server.
The value is
null
if the request is not yet complete or was unsuccessful, with the exception that when reading text data using a
responseType
of
"text"
or the empty string (
""
), the response can contain the response so far while the request is still in the
LOADING
readyState
(3).
The response types are described below.
""
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.
This example presents a function,
load()
, which loads and processes a page from the server. It works by creating an
XMLHttpRequest
object and creating a listener for
readystatechange
events such that that when
readyState
changes to
DONE
(4), the
response
is obtained and passed into the callback function provided to
load()
.
The content is handled as raw text data (since nothing here is overriding the default
responseType
).
var url = 'somePage.html'; //A local page
function load(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
callback(xhr.response);
}
}
xhr.open('GET', url, true);
xhr.send('');
}
| 规范 | 状态 | 注释 |
|---|---|---|
| XMLHttpRequest | 实时标准 | WHATWG (Web 超文本应用程序技术工作组) 实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
response
|
Chrome 9 | Edge 12 | Firefox 6 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 6 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 |
完整支持
XMLHttpRequest.responseText
and
XMLHttpRequest.responseXML
XMLHttpRequest