GlobalFetch

翻譯不完整。請協助 翻譯此英文文件

這是一個實驗中的功能
此功能在某些瀏覽器尚在開發中,請參考兼容表格以得到不同瀏覽器用的前輟。

The GlobalFetch interface of the Fetch API contains the GlobalFetch.fetch() method used to start the process of fetching a resource.

GlobalFetch is implemented by both Window and WorkerGlobalScope — meaning that it will be available in pretty much any context you might want to fetch resources in.

A fetch() promise will reject with a TypeError when a network error is encountered, although this usually means permissions issue or similar. An accurate check for a successful fetch() would include checking that the promise resolved, then checking that the Response.ok property has a value of true. An HTTP status of 404 does not constitute a network error.

GlobalFetch is controlled by the connect-src directive of Content Security Policy rather than the directive of the resources it's retrieving.

Note: The fetch() method's parameters are identical to those of the Request() constructor.

屬性

None.

方法

GlobalFetch.fetch()
Starts the process of fetching a resource.

範例

In our basic fetch example (run example live) we use a simple fetch call to grab an image and display it in an <img> tag. You'll notice that since we are requsting an image, we need to run Body.blob (Response implements body) to give the response its correct MIME type.

var myImage = document.querySelector('.my-image');
fetch('flowers.jpg').then(function(response) {
  return response.blob();
}).then(function(response) {
  var objectURL = URL.createObjectURL(response);
  myImage.src = objectURL;
});

規範

Specification Status Comment
Fetch
The definition of 'GlobalFetch' in that specification.
Living Standard Initial definition

瀏覽器相容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 42.0 39 (39)
34[1]
No support 29
28[1]
No support
Streaming response body 43.0 ? ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support 42.0 No support No support No support No support No support 42.0
Streaming response body No support 43.0 ? ? ? ? ? 43.0

[1] This API is implemented behind a preference.

參見

文件標籤與貢獻者

 此頁面的貢獻者: jackblackevo
 最近更新: jackblackevo,