Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
Fetch API 提供了一個獲取資源(包括跨網路)的介面。它是 XMLHttpRequest 的一個更強大且更有彈性的替代品。
Fetch API 使用 Request 和 Response 物件(以及其他涉及網路請求的事物),也使用相關的概念像是 CORS 和 HTTP Origin 標頭語意。
若要發出請求並獲取資源,請使用 fetch() 方法。它在 Window 和 Worker 上下文中都是全域方法。這使得它幾乎在你可能想要獲取資源的任何上下文中皆可用。
fetch() 方法接受一個強制性的引數,也就是你想要獲取的資源路徑。它會回傳一個 Promise,該 Promise 會解析為該請求的 Response——只要伺服器回應了標頭——即使伺服器回應是 HTTP 錯誤狀態。你也可以選擇性地傳入一個 init 選項物件作為第二個引數(參見 Request)。
一旦檢索到 Response,就有許多可用的方法來定義本體內容是什麼,以及應該如何處理它。
你可以直接使用 Request() 和 Response() 建構子來建立請求和回應,但通常不常這麼做。取而代之的是,這些更有可能作為其他 API 動作的結果而建立(例如來自 service workers 的 FetchEvent.respondWith())。
在使用 Fetch 中可以找到更多關於使用 Fetch API 功能的資訊。
fetchLater() API 讓開發者可以請求延遲獲取,它可以在指定的時間段後發送,或是當頁面關閉或導航離開時發送。參見使用延遲獲取。
用於獲取資源的 fetch() 方法。
Window.fetchLater()用於發出延遲獲取請求。
DeferredRequestInit代表可用於組態延遲獲取請求的選項集合。
FetchLaterResult代表請求延遲獲取的結果。
Headers代表回應/請求標頭,允許你查詢它們並根據結果採取不同的動作。
Request代表一個資源請求。
Response代表對請求的回應。
控制 fetchLater() API 的頂層配額。
deferred-fetch-minimal控制 fetchLater() API 的共享跨來源子框架配額。
| Fetch # fetch-method |
| Fetch # deferred-fetch |
Enable JavaScript to view this browser compatibility table.
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2026年2月26日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.