Created
April 22, 2019 19:01
-
-
Save ecancino/0ebeb7cedd90f6aa17e13acba7400614 to your computer and use it in GitHub Desktop.
Fetch explained
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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