aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/headers/headers/index.html
blob: 36cb2db4b9f4664dfa7c78a4b188325e4187a108 (plain)
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
---
title: Headers()
slug: Web/API/Headers/Headers
translation_of: Web/API/Headers/Headers
---
<div>{{APIRef("Fetch")}}</div>

<p><code><strong>Headers()</strong></code><strong> </strong>コンストラクターは新しい {{domxref("Headers")}} オブジェクトを生成します。</p>

<h2 id="文法">文法</h2>

<pre class="syntaxbox notranslate">var <em>myHeaders</em> = new Headers(<em>init</em>);</pre>

<h3 id="引数">引数</h3>

<dl>
 <dt><code>init</code> {{optional_inline}}</dt>
 <dd><code>Headers</code> に前もって設定したい <a href="/ja/docs/Web/HTTP/Headers">HTTP headers</a> を含むオブジェクト。これは {{domxref("ByteString")}} を持つ単純なオブジェクトリテラルか、既存の <code>Headers</code> オブジェクトのどちらかです。最後の例では、新しい <code>Headers</code> オブジェクトは既存の <code>Headers</code> オブジェクトからデータを継承します。</dd>
</dl>

<h2 id="例"></h2>

<p>簡単に空の <code>Headers</code> オブジェクトを生成できます。</p>

<pre class="brush: js notranslate">var myHeaders = new Headers(); // Currently empty</pre>

<p>{{domxref("Headers.append")}} を使用してヘッダーを追加できます。</p>

<pre class="brush: js notranslate">myHeaders.append('Content-Type', 'image/jpeg');
myHeaders.get('Content-Type'); // Returns 'image/jpeg'
</pre>

<p>または、<code>Headers</code> オブジェクトの生成時に必要なヘッダーを追加することもできます。次のスニペットでは、新しい {{domxref("Headers")}} を作成し、コンストラクターに init オブジェクトを引数として渡してヘッダーを追加しています。</p>

<pre class="brush: js notranslate">var httpHeaders = { 'Content-Type' : 'image/jpeg', 'Accept-Charset' : 'utf-8', 'X-My-Custom-Header' : 'Zeke are cool' };
var myHeaders = new Headers(httpHeaders);</pre>

<p>最初の <code>Headers</code> を init オブジェクトとして渡して、別の <code>Headers</code> オブジェクトを生成できます。</p>

<pre class="brush: js notranslate">var secondHeadersObj = new Headers(myHeaders);
secondHeadersObj.get('Content-Type'); // Would return 'image/jpeg' — it inherits it from the first headers object</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('Fetch','#dom-headers','Headers()')}}</td>
   <td>{{Spec2('Fetch')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="ブラウザの対応">ブラウザの対応</h2>

<p><br>
 {{Compat("api.Headers.headers")}}</p>

<h2 id="関連情報">関連情報</h2>

<ul>
 <li><a href="/ja/docs/Web/API/ServiceWorker_API">サービスワーカー API</a></li>
 <li><a href="/ja/docs/Web/HTTP/Access_control_CORS">HTTP アクセス制御 (CORS)</a></li>
 <li><a href="/ja/docs/Web/HTTP">HTTP</a></li>
</ul>