XMLHttpRequest.response
XMLHttpRequest
response
プロパティは、そのリクエストのresponseType
によって、ArrayBuffer
, Blob
, Document
, JavaScript Object
, or DOMString
といったレスポンスのボディを返します。
構文
var body = XMLHttpRequest.response;
値
responseType
の値に基づく適切なオブジェクト。 open()
を呼び出してリクエストを初期化した後や、send()
を呼び出してリクエストをサーバーに送信する前に、responseType
の値を設定することで、特定の形式でデータを提供するようにリクエストができます。
リクエストが未完了または失敗する場合、値はnull
です。ただし、"text"
や空の文字列である(""
)を使用してテキストデータを読み込む場合は除きます。リクエストがまだLOADING
readyState
(3)にある間、レスポンスはこれまでのレスポンスを含むことがあります。
レスポンスタイプは以下のとおりです。
""
- An empty
responseType
string is treated the same as"text"
, the default type. arraybuffer
- The
response
is a JavaScriptArrayBuffer
containing binary data. blob
- The
response
is aBlob
object containing the binary data. document
- The
response
is an HTMLDocument
or XMLXMLDocument
, as appropriate based on the MIME type of the received data. See HTML in XMLHttpRequest to learn more about using XHR to fetch HTML content. json
- The
response
is a JavaScript object created by parsing the contents of received data as JSON. text
- The
response
is a text in aDOMString
object. ms-stream
- The
response
is part of a streaming download; this response type is only allowed for download requests, and is only supported by Internet Explorer.
例
下記に、サーバーからページをロードして処理する関数load()
を例として提示します。仕組みとしては、XMLHttpRequestオブジェクトを作成し、readystatechange
イベントのリスナー(readyState
イベントがDONE
(4)に変わるとresponse
が取得され、それをload()
のコールバック関数に渡すといった)を作成しています。
コンテンツは生のテキストデータとして処理されます(デフォルトの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 living standard |
ブラウザの対応
BCD tables only load in the browser
関連
- Using XMLHttpRequest
- Getting text and HTML/XML data:
XMLHttpRequest.responseText
andXMLHttpRequest.responseXML