diff options
Diffstat (limited to 'files/zh-cn/web/api/response/headers/index.html')
-rw-r--r-- | files/zh-cn/web/api/response/headers/index.html | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/response/headers/index.html b/files/zh-cn/web/api/response/headers/index.html new file mode 100644 index 0000000000..935518d788 --- /dev/null +++ b/files/zh-cn/web/api/response/headers/index.html @@ -0,0 +1,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> |