Houve um erro de script nesta página. Enquanto este erro é encaminhado para os editores do site, você pode ver o conteúdo parcial abaixo.
Esta tradução está incompleta. Ajude atraduzir este artigo.
{{DefaultAPISidebar("Fetch API")}}{{ SeeCompatTable() }}
The Fetch API provides an interface for fetching resources (e.g., across the network.) It will seem familiar to anyone who has used {{domxref("XMLHttpRequest")}}, but the new API provides a more powerful and flexible feature set.
Conceitos e uso
Fetch provides a generic definition of {{domxref("Request")}} and {{domxref("Response")}} objects (and other things involved with network requests). This will allow them to be used wherever they are needed in the future, whether it’s for service workers, Cache API and other similar things that handle or modify requests and responses, or any kind of use case that might require you to generate your own responses programmatically.
It also provides a definition for related concepts such as CORS and the HTTP origin header semantics, supplanting their separate definitions elsewhere.
For making a request and fetching a resource, use the {{domxref("GlobalFetch.fetch")}} method. It is implemented in multiple interfaces, specifically {{domxref("Window")}} and {{domxref("WorkerGlobalScope")}}. This makes it available in pretty much any context you might want to fetch resources in.
The fetch() method takes one mandatory argument, the path to the resource you want to fetch. It returns a promise that resolves to the {{domxref("Response")}} to that request, whether it is successful or not. You can also optionally pass in an init options object as the second argument (see {{domxref("Request")}}.)
Once a {{domxref("Response")}} is retrieved, there are a number of methods available to define what the body content is and how it should be handled (see {{domxref("Body")}}.)
You can create a request and response directly using the {{domxref("Request.Request","Request()")}} and {{domxref("Response.Response","Response()")}} constructors, but you are unlikely to do this directly. Instead, these are more likely to be created as results of other API actions (for example, {{domxref("FetchEvent.respondWith")}} from service workers.)
Note: Encontre mais informações sobre os recursos do Fetch API em Using Fetch, e conceitos para estudos em Fetch basic concepts.
Fetch Interfaces
- {{domxref("GlobalFetch")}}
- Contains the
fetch()method used to fetch a resource. - {{domxref("Headers")}}
- Represents response/request headers, allowing you to query them and take different actions depending on the results.
- {{domxref("Request")}}
- Represents a resource request.
- {{domxref("Response")}}
- Representa a resposta de uma requisição.
Fetch mixin
- {{domxref("Body")}}
- Provides methods relating to the body of the response/request, allowing you to declare what its content type is and how it should be handled.
Especificações
| Especificação | Status | Comentários |
|---|---|---|
| {{SpecName('Fetch')}} | {{Spec2('Fetch')}} | Initial definition |
Navegadores Compatíveis
{{ CompatibilityTable() }}
| Recurso | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Suporte Básico | {{ CompatChrome(42.0) }} |
{{ CompatGeckoDesktop(39)}} 34 behind pref |
{{ "Sem compatibilidade" }} |
29 |
{{ "Sem compatibilidade" }} {{webkitbug(151937)}} |
| Streaming response body | {{CompatChrome(43.0)}} |
{{Desconhecido}} |
{{Desconhecido}} |
{{Desconhecido}} |
{{Desconhecido}} |
| Recurso | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Suporte Básico | {{ "Sem compatibilidade" }} | {{CompatChrome(42.0)}} | {{ "Sem compatibilidade" }} | {{ "Sem compatibilidade" }} | {{ "Sem compatibilidade" }} | {{ "Sem compatibilidade" }} | {{ "Sem compatibilidade"}} | {{ CompatChrome(42.0) }} |
| Streaming respone body | {{ "Sem compatibilidade"}} | {{CompatChrome(43.0)}} |
{{Desconhecido}} |
{{Desconhecido}} |
{{Desconhecido}} |
{{Desconhecido}} |
{{Desconhecido}} |
{{CompatChrome(43.0)}} |