XMLHttpRequest: responseText プロパティ
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
XMLHttpRequest の responseText プロパティは読み取り専用で、送信されたリクエストに続いてサーバーから受け取ったテキストを返します。
値
文字列で、 XMLHttpRequest を使用して受信したテキストデータ、またはリクエストが失敗した場合もしくはまだコンテンツが受信されていない場合は "" です。
非同期リクエストを処理している間、 responseText の値は、データが完全に受信できておらず不完全であっても、常にサーバーから受信した現在のコンテンツを持ちます。
readyState の値が XMLHttpRequest.DONE (4) になり、 status の値が 200 ("OK") になった場合、コンテンツ全体が受信されたことが分かります。
例外
InvalidStateErrorDOMException-
XMLHttpRequest.responseTypeが空文字列または"text"のどちらにも設定されていません。responseTextプロパティはテキストコンテンツのみで有効なので、他の値はエラーの状態です。
例
js
const xhr = new XMLHttpRequest();
xhr.open("GET", "http://developer.mozilla.org/server", true);
// If specified, responseType must be empty string or "text"
xhr.responseType = "text";
xhr.onload = () => {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
console.log(xhr.response);
console.log(xhr.responseText);
}
}
};
xhr.send(null);
仕様書
| Specification |
|---|
| XMLHttpRequest> # the-responsetext-attribute> |
ブラウザーの互換性
Loading…