Tutorial by Examples

The GlobalFetch interface exposes the fetch function, which can be used to request resources. fetch('/path/to/resource.json') .then(response => { if (!response.ok()) { throw new Error("Request failed!"); } return response.json(...
fetch('/example.json', { headers: new Headers({ 'Accept': 'text/plain', 'X-Your-Custom-Header': 'example value' }) });
Posting form data fetch(`/example/submit`, { method: 'POST', body: new FormData(document.getElementById('example-form')) }); Posting JSON data fetch(`/example/submit.json`, { method: 'POST', body: JSON.stringify({ email: document.getElementById('example-email').val...
The fetch function does not send cookies by default. There are two possible ways to send cookies: Only send cookies if the URL is on the same origin as the calling script. fetch('/login', { credentials: 'same-origin' }) Always send cookies, even for cross-origin calls. fetch('htt...
// get some data from stackoverflow fetch("https://api.stackexchange.com/2.2/questions/featured?order=desc&sort=activity&site=stackoverflow") .then(resp => resp.json()) .then(json => console.log(json)) .catch(err => console.log(err));
const url = 'http://api.stackexchange.com/2.2/questions?site=stackoverflow&tagged=javascript'; const questionList = document.createElement('ul'); document.body.appendChild(questionList); const responseData = fetch(url).then(response => response.json()); responseData.then(({item...

Page 1 of 1