1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
---
title: XMLHttpRequest.response
slug: Web/API/XMLHttpRequest/response
tags:
- AJAX
- API
- Content
- Fetching
- Fetching Data
- Loading Data
- Property
- Read-only
- Reading Data
- Reference
- Response
- Server
- XMLHttpRequest
translation_of: Web/API/XMLHttpRequest/response
---
<div>{{APIRef('XMLHttpRequest')}}</div>
<p><span class="seoSummary">{{domxref("XMLHttpRequest")}} <code><strong>response</strong></code> プロパティは、そのリクエストの{{domxref("responseType")}}によって、{{domxref("ArrayBuffer")}}, {{domxref("Blob")}}, {{domxref("Document")}}, JavaScript {{jsxref("Object")}}, or {{domxref("DOMString")}}といったレスポンスのボディを返します。</span></p>
<h2 id="構文">構文</h2>
<pre class="syntaxbox">var <em>body</em> = <em>XMLHttpRequest</em>.response;
</pre>
<h3 id="値">値</h3>
<p>{{domxref("XMLHttpRequest.responseType", "responseType")}}の値に基づく適切なオブジェクト。 {{domxref("XMLHttpRequest.open", "open()")}}を呼び出してリクエストを初期化した後や、{{domxref("XMLHttpRequest.send", "send()")}}を呼び出してリクエストをサーバーに送信する前に、<code>responseType</code>の値を設定することで、特定の形式でデータを提供するようにリクエストができます。</p>
<p>リクエストが未完了または失敗する場合、値は<code>null</code>です。ただし、<code>"text"</code>や空の文字列である(<code>""</code>)を使用してテキストデータを読み込む場合は除きます。リクエストがまだ<code>LOADING</code> {{domxref("XMLHttpRequest.readyState", "readyState")}} (3)にある間、レスポンスはこれまでのレスポンスを含むことがあります。</p>
<p>レスポンスタイプは以下のとおりです。</p>
<p>{{page("/en-US/docs/Web/API/XMLHttpRequestResponseType", "Values")}}</p>
<dl>
</dl>
<h2 id="例">例</h2>
<p>下記に、サーバーからページをロードして処理する関数<code>load()</code>を例として提示します。仕組みとしては、XMLHttpRequestオブジェクトを作成し、{{event("readystatechange")}}イベントのリスナー(<code>readyState</code>イベントが<code>DONE</code> (4)に変わると<code>response</code>が取得され、それを<code>load()</code>のコールバック関数に渡すといった)を作成しています。</p>
<p>コンテンツは生のテキストデータとして処理されます(デフォルトの{{domxref("XMLHttpRequest.responseType", "responseType")}}を上書きするものは何もないため)。</p>
<pre class="brush: js">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('');
}
</pre>
<h2 id="仕様書">仕様書</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">仕様書</th>
<th scope="col">状態</th>
<th scope="col">備考</th>
</tr>
<tr>
<td>{{SpecName('XMLHttpRequest', '#the-response-attribute')}}</td>
<td>{{Spec2('XMLHttpRequest')}}</td>
<td>WHATWG living standard</td>
</tr>
</tbody>
</table>
<h2 id="ブラウザの対応">ブラウザの対応</h2>
<p>{{Compat("api.XMLHttpRequest.response")}}</p>
<h2 id="関連">関連</h2>
<ul>
<li><a href="/ja/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest">Using XMLHttpRequest</a></li>
<li>Getting text and HTML/XML data: {{domxref("XMLHttpRequest.responseText")}} and {{domxref("XMLHttpRequest.responseXML")}}</li>
</ul>
|