From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../getallresponseheaders/index.html | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 files/ja/web/api/xmlhttprequest/getallresponseheaders/index.html (limited to 'files/ja/web/api/xmlhttprequest/getallresponseheaders') diff --git a/files/ja/web/api/xmlhttprequest/getallresponseheaders/index.html b/files/ja/web/api/xmlhttprequest/getallresponseheaders/index.html new file mode 100644 index 0000000000..087a42f29b --- /dev/null +++ b/files/ja/web/api/xmlhttprequest/getallresponseheaders/index.html @@ -0,0 +1,123 @@ +--- +title: XMLHttpRequest.getAllResponseHeaders() +slug: Web/API/XMLHttpRequest/getAllResponseHeaders +tags: + - API + - Fetch Headers + - Get Headers + - HTTP + - HTTP Header + - Method + - Reference + - Response Header + - XHR + - XMLHttpRequest + - getAllResponseHeaders +translation_of: Web/API/XMLHttpRequest/getAllResponseHeaders +--- +
{{APIRef('XMLHttpRequest')}}
+ +

{{domxref("XMLHttpRequest")}} の getAllResponseHeaders() メソッドは、すべてのレスポンスヘッダーを {{Glossary('CRLF')}} で区切った文字列として返し、レスポンスを受信していない場合は null を返します。ネットワークエラーが発生した場合は、空文字列が返されます。

+ +
+

注: マルチパートリクエストでは、これはリクエストの元のチャンネルではなく、現在の部分を返します。

+
+ +

構文

+ +
var headers = XMLHttpRequest.getAllResponseHeaders();
+ +

引数

+ +

なし。

+ +

返値

+ +

{{Glossary('CRLF')}} で区切ったすべてのレスポンスヘッダー (フィールド名が Set-Cookie または Set-Cookie2 のものを除く) を表す {{domxref("ByteString")}}、またはレスポンスを受信していなければ null です。ネットワークエラーが発生した場合は、空文字列が返されます。

+ +

生のヘッダー文字列がどのように見えるかの例です。

+ +
date: Fri, 08 Dec 2017 21:04:30 GMT\r\n
+content-encoding: gzip\r\n
+x-content-type-options: nosniff\r\n
+server: meinheld/0.6.1\r\n
+x-frame-options: DENY\r\n
+content-type: text/html; charset=utf-8\r\n
+connection: keep-alive\r\n
+strict-transport-security: max-age=63072000\r\n
+vary: Cookie, Accept-Encoding\r\n
+content-length: 6502\r\n
+x-xss-protection: 1; mode=block\r\n
+ +

各行はキャリッジリターンとラインフィード文字 (\r\n) の両方で終わります。これらはそれぞれのヘッダーを区切る基本的なデリミターです。

+ +
+

: 最近のブラウザーでは、ヘッダー名は最新の仕様書にあるように、すべて小文字で返されます。

+
+ +

+ +

この例では、リクエストの {{event("readystatechange")}} イベントハンドラーである {{domxref("XMLHttpRequest.onreadystatechange")}} の中でヘッダーを調べます。このコードは生のヘッダー文字列を取得する方法、またそれを個別のヘッダーの配列に変換する方法、そして配列からヘッダー名とその値のマップを生成する方法を示しています。

+ +
var request = new XMLHttpRequest();
+request.open("GET", "foo.txt", true);
+request.send();
+
+request.onreadystatechange = function() {
+  if(this.readyState == this.HEADERS_RECEIVED) {
+
+    // 生のヘッダー文字列を取得
+    var headers = request.getAllResponseHeaders();
+
+    // ヘッダー文字列を個別のヘッダーの
+    // 配列に変換
+    var arr = headers.trim().split(/[\r\n]+/);
+
+    // ヘッダー名と値のマップを生成
+    var headerMap = {};
+    arr.forEach(function (line) {
+      var parts = line.split(': ');
+      var header = parts.shift();
+      var value = parts.join(': ');
+      headerMap[header] = value;
+    });
+  }
+}
+ +

いったんこれを行えば、次のようなことができます。

+ +
var contentType = headerMap["content-type"];
+ +

これは {{httpheader("Content-Type")}} ヘッダーの値を変数 contentType の中に入れます。

+ +

仕様書

+ + + + + + + + + + + + + + + + +
仕様書状態備考
{{SpecName('XMLHttpRequest', '#the-getallresponseheaders()-method', 'getAllResponseHeaders()')}}{{Spec2('XMLHttpRequest')}}WHATWG living standard
+ +

ブラウザーの互換性

+ + + +
{{Compat("api.XMLHttpRequest.getAllResponseHeaders")}}
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf