window.fetch

fetch — Fetches a resource from the web.

Description

fetch(mixed resource, [object settings])

Parameters

Name Description Type Default Optional
resource url or object. mixed No
settings object Yes
Settings parameters
method

Request method, e.g., GET, POST, HEAD, …

headers

Custom headers for the request.

body

The body for the request.

mode

Request mode, e.g., cors, no-cors, same-origin, …

credentials

Custom credentials for the request.

cache

Request cache, e.g., no-store, reload, no-cache, …

redirect

Request redirect: follow, error, manual.

referrer

Request referrer: no-referrer, client or URL

referrerPolicy

Request referrer policy, e.g., no-referrer, origin, unsafe-url, …

integrity

A cryptographic hash that the request must match.

keepalive

Allow the request to stay alive.

signal

A signal to communicate with the fetch process.

Return values

new Promise

Examples

Example #1 – fetch example
let result = fetch('https://www.jsman.net/humans.txt'); result.then(result => { return result.text(); }).then(result => { console.log(result); });
Example #2 – fetch error example
let result = fetch('https://www.jsman.net/humans.txt.notfound'); result.then(result => { if (!result.ok) { throw new Error('Got responce code: ' + result.status); } return result.text(); }).then(result => { console.log(result); }).catch(error => { console.log(error.message); });

See also

External references