Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

GlobalFetch.fetch()

この翻訳は不完全です。英語から この記事を翻訳 してください。

これは実験段階の機能です。
この機能は複数のブラウザで開発中の状態にあります。各ブラウザで用いるために、適切なベンダー接頭辞が必要な場合があります。互換性テーブルをチェックしてください。また、実験段階の機能の構文と挙動は、仕様変更に伴い各ブラウザの将来のバージョンで変更になる可能性があることに注意してください。

GlobalFetch interfaceのfetch()メソッドはリソースのfetchを開始します。 これは fetchのレスポンスを表すResponseオブジェクトのPromiseを返します。

GlobalFetchは WindowWorkerGlobalScope の両方で実装されています。つまりfetch()メソッドは もうすぐあなたがfetchしたいと思った大半のコンテキストで使用可能になります

fetch() のpromiseはネットワークエラーの場合 TypeError としてreject状態になるでしょう。しかし TypeError は普通パーミッションの問題やそれに似た問題で発生します。fetch()が成功の為の精密なチェックとしてpromiseがresolvedかどうかを確認しResponse.okプロパティがtureかどうかを確認します。 HTTP statusコードが404の場合はネットワークエラーではありません。

fetch()メソッドは取得するリソースのディレクティブではなく Content Security Policyconnect-src ディレクティブによって制御されます。

fetch() メソッドの引数は Request() コンストラクタと全く同じです。

構文

fetch(input, init).then(function(response) { ... });

引数

input
あなたがfetchしたいリソースを定義します。以下の2つが使用出来ます。
  • あなたがfetchしたいURLを含む USVString
  • A Requestオブジェクト。
init Optional
あなたがリクエストに適用したい設定を含むオプションの(必須ではない)オブジェクトです。以下のオプションが使用できます。
  • method: リクエストするメソッド, GET, POSTなど。
  • headers :あなたがリクエストに追加したいヘッダー、 Headers オブジェクトか ByteStringで指定して下さい。
  • body: あなたがリクエストに追加したいbody、 Blob, BufferSource, FormData, URLSearchParams, or USVString オブジェクトなどが使えます。methodがGETやHEADの場合使用できないので注意して下さい。
  • mode: あなたがリクエストで使いたいモードです。例:cors, no-cors, or same-origin.
  • credentials: The request credentials you want to use for the request: omit, same-origin, or include. To automatically send cookies for the current domain, this option must be provided.
  • cache:あなたが使用したいリクエストのキャッシュのモードを指定できます。: 通常は, no-store, reload, no-cache, force-cache, only-if-cached.
  • redirect: The redirect mode to use: follow (automatically follow redirects), error (abort with an error if a redirect occurs), or manual (handle redirects manually). In Chrome the default was follow before Chrome 47 and manual starting with Chrome 47.
  • referrer: A USVString specifying no-referrerclient, or a URL. The default is client.
  • referrerPolicy: Specifies the value of the referer HTTP header. May be one of no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, unsafe-url.
  • integrity: Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).

返り値

解決時に Response オブジェクトを取得できる Promise オブジェクト。

例外

Type Description
TypeError  Firefox 43 から,もしhttps://user:[email protected]のようにURLにcredentialsを含む場合、 fetch() はTypeError を投げます。

 Fetch Request example リポジトリ(デモ: Fetch Request live) ではRequestオブジェクトを関連するコンストラクタを使って作成しています。その後fetch()を呼んでいます。画像をfetchingしている時から適切なMIMEタイプを受け取るために responseのBody.blob を実行し、 適切に処理されます。そしてObjectURLを作成し <img> 要素に追加して表示させています。

var myImage = document.querySelector('img');

var myRequest = new Request('flowers.jpg');

fetch(myRequest).then(function(response) {
  return response.blob();
}).then(function(response) {
  var objectURL = URL.createObjectURL(response);
  myImage.src = objectURL;
});

 Fetch with init then Request example リポジトリ(デモ: Fetch Request init live) では上記の内容に加えて、fetch()を呼び出すとき、初期化オブジェクトinitを渡しています。

var myImage = document.querySelector('img');

var myHeaders = new Headers();
myHeaders.append('Content-Type', 'image/jpeg');

var myInit = { method: 'GET',
               headers: myHeaders,
               mode: 'cors',
               cache: 'default' };

var myRequest = new Request('flowers.jpg');

fetch(myRequest,myInit).then(function(response) {
  ... 
});

同様に初期化オブジェクトを Request コンストラクタに渡しても、同じ効果が得られます。例:

var myRequest = new Request('flowers.jpg',myInit);

仕様

Specification 状態 Comment
Fetch
fetch() の定義
現行の標準 Initial definition
Credential Management Level 1 勧告改訂案 Adds FederatedCredential or PasswordCredential instance as a possible value for init.credentials.

ブラウザ互換性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 42.0 39 (39)
34[1]
未サポート 29
28[1]
未サポート
Streaming response body 43.0 ? ? ? ?
Support for blob: and data: 48.0        
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support 未サポート 42.0 未サポート 未サポート 未サポート 未サポート 未サポート 42.0
Streaming response body 未サポート 43.0 ? ? ? ? ? 43.0
Support for blob: and data: 未サポート 43.0  

関連項目

ドキュメントのタグと貢献者

 このページの貢献者: chikoski, lv7777
 最終更新者: chikoski,