Skip to content

Instantly share code, notes, and snippets.

@ecancino
Created April 22, 2019 19:01
Show Gist options
  • Select an option

  • Save ecancino/0ebeb7cedd90f6aa17e13acba7400614 to your computer and use it in GitHub Desktop.

Select an option

Save ecancino/0ebeb7cedd90f6aa17e13acba7400614 to your computer and use it in GitHub Desktop.
Fetch explained
function addUser(details) {
return fetch('https://api.example.com/user', {
mode: 'cors',
method: 'POST',
credentials: 'include',
body: JSON.stringify(details),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-XSRF-TOKEN': getCookieValue('XSRF-TOKEN')
}
}).then(response => {
return response.json().then(data => {
if (response.ok) {
return data;
} else {
return Promise.reject({ status: response.status, data });
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment