aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/response/headers/index.html
blob: 935518d788421afb56176b793466320ca84a90da (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
---
title: Response.headers
slug: Web/API/Response/headers
translation_of: Web/API/Response/headers
---
<div>{{APIRef("Fetch")}}</div>

<p>{{domxref("Response")}} 接口的只读属性 <code><strong>headers</strong></code> 包含与响应关联的{{domxref("Headers")}}对象。</p>

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

<pre class="syntaxbox">var <var>myHeaders</var> = <var>response</var>.headers;</pre>

<h3 id="值"></h3>

<p>一个 {{domxref("Headers")}} 对象。</p>

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

<p>在我们的 <a href="https://github.com/mdn/fetch-examples/tree/gh-pages/fetch-response">Fetch Response example</a> 例程中(详见 <a href="http://mdn.github.io/fetch-examples/fetch-response/">Fetch Response live</a>),我们使用{{domxref("Request.Request","Request()")}}构造函数创建了一个新的{{domxref("Request")}}对象,传入了一个jpg路径。我们接着使用{{domxref("GlobalFetch.fetch","fetch()")}}触发了请求,用{{domxref("Body.blob")}}从响应中提取了blob实例,使用{{domxref("URL.createObjectURL")}}创建了一个URL对象,然后显示在了{{htmlelement("img")}}中。</p>

<p>注意,在<code>fetch()</code>的顶级块中我们输出了<code>headers</code>到控制台。</p>

<pre class="brush: js">var myImage = document.querySelector('img');

var myRequest = new Request('flowers.jpg');

fetch(myRequest).then(function(response) {
  console.log(response.headers); // returns a Headers{} object
  response.blob().then(function(myBlob) {
    var objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });
});</pre>

<h2 id="规格">规格</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Fetch','#dom-response-headers','headers')}}</td>
   <td>{{Spec2('Fetch')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

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

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li>
 <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
</ul>