blob: 8899c22fa5cd823a5d137752a45c1820bd9255aa (
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
---
title: Response
slug: Web/API/Response
translation_of: Web/API/Response
---
<div>{{APIRef("Fetch API")}}</div>
<p><a href="/en-US/docs/Web/API/Fetch_API">Fetch API</a> 的 <strong><code>Response</code></strong> 介面代表了一個請求會返回的回應。</p>
<p>你可以用 {{domxref("Response.Response()")}} 建構子創建一個新的 <code>Response</code>物件。但實務上碰到 Response 物件的時機,比較常出現在進行了一個 API 操作後,得到返回的 Response 物件。舉例而言,使用 service worker {{domxref("Fetchevent.respondWith")}} 或是使用單純的{{domxref("GlobalFetch.fetch()")}}。</p>
<h2 id="建構子">建構子</h2>
<dl>
<dt>{{domxref("Response.Response","Response()")}}</dt>
<dd>創建一個新的 <code>Response</code> 物件。</dd>
</dl>
<h2 id="屬性">屬性</h2>
<dl>
<dt>{{domxref("Response.headers")}} {{readonlyinline}}</dt>
<dd>包含與此 response 相關的 {{domxref("Headers")}} 物件。</dd>
<dt>{{domxref("Response.ok")}} {{readonlyinline}}</dt>
<dd>無論此 response 是不是成功 ( 狀態碼 200-299 ) 都會包含一個 boolean 狀態。</dd>
<dt>{{domxref("Response.redirected")}} {{ReadOnlyInline}}</dt>
<dd>指出此 response 是不是個重新導向的結果,如果是的話,代表此 URL 具有一個以上的入口。</dd>
<dt>{{domxref("Response.status")}} {{readonlyinline}}</dt>
<dd>包含此 response 的狀態碼(例如:成功時為 <code>200</code> )。</dd>
<dt>{{domxref("Response.statusText")}} {{readonlyinline}}</dt>
<dd>包含狀態碼所對應的狀態文字 (例如: <code>OK</code> 對應 <code>200</code>)。</dd>
<dt>{{domxref("Response.type")}} {{readonlyinline}}</dt>
<dd>包含此 response 的類型 (例如: <code>basic</code>, <code>cors</code>)。</dd>
<dt>{{domxref("Response.url")}} {{readonlyinline}}</dt>
<dd>包含此 response 的 URL。</dd>
<dt>{{domxref("Response.useFinalURL")}}</dt>
<dd>包含一個 boolean 狀態,指出此 URL 是否為此 response 的最後一步。</dd>
</dl>
<p><code>Response</code> 實做了{{domxref("Body")}}, 所以它另有以下可用的屬性:</p>
<dl>
<dt>{{domxref("Body.body")}} {{readonlyInline}}</dt>
<dd>A simple getter used to expose a {{domxref("ReadableStream")}} of the body contents.</dd>
<dt>{{domxref("Body.bodyUsed")}} {{readonlyInline}}</dt>
<dd>Stores a {{domxref("Boolean")}} that declares whether the body has been used in a response yet.</dd>
</dl>
<h2 id="方法">方法</h2>
<dl>
<dt>{{domxref("Response.clone()")}}</dt>
<dd>建立一份 <code>Response</code> 的複製物件。</dd>
<dt>{{domxref("Response.error()")}}</dt>
<dd>Returns a new <code>Response</code> object associated with a network error.</dd>
<dt>{{domxref("Response.redirect()")}}</dt>
<dd>Creates a new response with a different URL.</dd>
</dl>
<p><code>Response</code> implements {{domxref("Body")}}, so it also has the following methods available to it:</p>
<dl>
<dt>{{domxref("Body.arrayBuffer()")}}</dt>
<dd>Takes a {{domxref("Response")}} stream and reads it to completion. It returns a promise that resolves with an {{domxref("ArrayBuffer")}}.</dd>
<dt>{{domxref("Body.blob()")}}</dt>
<dd>Takes a {{domxref("Response")}} stream and reads it to completion. It returns a promise that resolves with a {{domxref("Blob")}}.</dd>
<dt>{{domxref("Body.formData()")}}</dt>
<dd>Takes a {{domxref("Response")}} stream and reads it to completion. It returns a promise that resolves with a {{domxref("FormData")}} object.</dd>
<dt>{{domxref("Body.json()")}}</dt>
<dd>Takes a {{domxref("Response")}} stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as {{jsxref("JSON")}}.</dd>
<dt>{{domxref("Body.text()")}}</dt>
<dd>Takes a {{domxref("Response")}} stream and reads it to completion. It returns a promise that resolves with a {{domxref("USVString")}} (text).</dd>
</dl>
<h2 id="範例">範例</h2>
<p>在<a href="https://github.com/mdn/fetch-examples/tree/master/basic-fetch">基本 fetch 範例</a> (<a href="http://mdn.github.io/fetch-examples/basic-fetch/">run example live</a>) 中,我們使用<span style="font-size: 1rem; letter-spacing: -0.00278rem;"> </span><code style="font-size: 1rem; letter-spacing: -0.00278rem;">fetch()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;"> 呼叫來得到圖片,並使用 {{htmlelement("img")}} tag 顯示。 這裡的</span><code style="font-size: 1rem; letter-spacing: -0.00278rem;">fetch()</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;"> 呼叫返回了一個 promise,它使用與資源 fetch 操作有關的 </span><code style="font-size: 1rem; letter-spacing: -0.00278rem;">Response</code><span style="font-size: 1rem; letter-spacing: -0.00278rem;"> 進行解析。你可能有發現,由於我們要求的是一張圖片,所以需要用 {{domxref("Body.blob")}} ({{domxref("Response")}} 時做了 body) 讓 response 有正確的 MIME 類型。</span></p>
<pre class="brush: js">const image = document.querySelector('.my-image');
fetch('flowers.jpg').then(function(response) {
return response.blob();
}).then(function(blob) {
const objectURL = URL.createObjectURL(blob);
image.src = objectURL;
});</pre>
<p>除此之外,你也能用 {{domxref("Response.Response()")}} 建構子去建立自己的客製化 <code>Response</code> 物件:</p>
<pre class="brush: js">const response = new Response();</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','#response-class','Response')}}</td>
<td>{{Spec2('Fetch')}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{Compat("api.Response")}}</p>
<h2 id="參考">參考</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>
|