pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/zh-TW/docs/Web/API/Fetch_API

ript src="/static/client/runtime.acb3350e06728bd3.js" type="module">

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

View in English Always switch to English

Fetch API

Fetch API 提供了一個獲取資源(包括跨網路)的介面。它是 XMLHttpRequest 的一個更強大且更有彈性的替代品。

概念與用法

Fetch API 使用 RequestResponse 物件(以及其他涉及網路請求的事物),也使用相關的概念像是 CORS 和 HTTP Origin 標頭語意。

若要發出請求並獲取資源,請使用 fetch() 方法。它在 WindowWorker 上下文中都是全域方法。這使得它幾乎在你可能想要獲取資源的任何上下文中皆可用。

fetch() 方法接受一個強制性的引數,也就是你想要獲取的資源路徑。它會回傳一個 Promise,該 Promise 會解析為該請求的 Response——只要伺服器回應了標頭——即使伺服器回應是 HTTP 錯誤狀態。你也可以選擇性地傳入一個 init 選項物件作為第二個引數(參見 Request)。

一旦檢索到 Response,就有許多可用的方法來定義本體內容是什麼,以及應該如何處理它。

你可以直接使用 Request()Response() 建構子來建立請求和回應,但通常不常這麼做。取而代之的是,這些更有可能作為其他 API 動作的結果而建立(例如來自 service workers 的 FetchEvent.respondWith())。

使用 Fetch 中可以找到更多關於使用 Fetch API 功能的資訊。

延遲獲取

fetchLater() API 讓開發者可以請求延遲獲取,它可以在指定的時間段後發送,或是當頁面關閉或導航離開時發送。參見使用延遲獲取

介面

Window.fetch()WorkerGlobalScope.fetch()

用於獲取資源的 fetch() 方法。

Window.fetchLater()

用於發出延遲獲取請求。

DeferredRequestInit

代表可用於組態延遲獲取請求的選項集合。

FetchLaterResult

代表請求延遲獲取的結果。

Headers

代表回應/請求標頭,允許你查詢它們並根據結果採取不同的動作。

Request

代表一個資源請求。

Response

代表對請求的回應。

HTTP 標頭

deferred-fetch

控制 fetchLater() API 的頂層配額

deferred-fetch-minimal

控制 fetchLater() API 的共享跨來源子框架配額

規範

Specification
Fetch
# fetch-method
Fetch
# deferred-fetch

瀏覽器相容性

api.fetch

api.Window.fetchLater

參見

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy