Movatterモバイル変換


[0]ホーム

URL:


  1. 給開發者的 Web 技術文件
  2. Web API
  3. Response

此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in EnglishAlways switch to English

Response

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2017年3月.

* Some parts of this feature may have varying levels of support.

Fetch APIResponse 介面代表了一個請求會返回的回應。

你可以用Response.Response() 建構子創建一個新的Response物件。但實務上碰到 Response 物件的時機,比較常出現在進行了一個 API 操作後,得到返回的 Response 物件。舉例而言,使用 service workerFetchevent.respondWith 或是使用單純的GlobalFetch.fetch()

建構子

Response()

創建一個新的Response 物件。

屬性

Response.headersRead only

包含與此 response 相關的Headers 物件。

Response.okRead only

無論此 response 是不是成功 ( 狀態碼 200-299 ) 都會包含一個 boolean 狀態。

Response.redirectedRead only

指出此 response 是不是個重新導向的結果,如果是的話,代表此 URL 具有一個以上的入口。

Response.statusRead only

包含此 response 的狀態碼(例如:成功時為200 )。

Response.statusTextRead only

包含狀態碼所對應的狀態文字 (例如:OK 對應200)。

Response.typeRead only

包含此 response 的類型 (例如:basic,cors)。

Response.urlRead only

包含此 response 的 URL。

Response.useFinalURL

包含一個 boolean 狀態,指出此 URL 是否為此 response 的最後一步。

Response 實做了Body, 所以它另有以下可用的屬性:

Body.bodyRead only

A simple getter used to expose aReadableStream of the body contents.

Body.bodyUsedRead only

Stores aBoolean that declares whether the body has been used in a response yet.

方法

Response.clone()

建立一份Response 的複製物件。

Response.error()

Returns a newResponse object associated with a network error.

Response.redirect()

Creates a new response with a different URL.

Response implementsBody, so it also has the following methods available to it:

Body.arrayBuffer()

Takes aResponse stream and reads it to completion. It returns a promise that resolves with anArrayBuffer.

Body.blob()

Takes aResponse stream and reads it to completion. It returns a promise that resolves with aBlob.

Body.formData()

Takes aResponse stream and reads it to completion. It returns a promise that resolves with aFormData object.

Body.json()

Takes aResponse stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text asJSON.

Body.text()

Takes aResponse stream and reads it to completion. It returns a promise that resolves with aUSVString (text).

範例

基本 fetch 範例 (run example live) 中,我們使用fetch() 呼叫來得到圖片,並使用<img> tag 顯示。 這裡的fetch() 呼叫返回了一個 promise,它使用與資源 fetch 操作有關的Response 進行解析。你可能有發現,由於我們要求的是一張圖片,所以需要用Body.blob (Response 時做了 body) 讓 response 有正確的 MIME 類型。

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;  });

除此之外,你也能用Response.Response() 建構子去建立自己的客製化Response 物件:

js
const response = new Response();

規範

Specification
Fetch
# response-class

瀏覽器相容性

參考

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp