Last active
May 21, 2020 12:23
-
-
Save justsml/13915347d6c8413c73f4bd7240c68e51 to your computer and use it in GitHub Desktop.
fetch examples: https://danlevy.net/you-may-not-need-axios/
This file contains 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
postRequest('http://example.com/api/v1/users', {user: 'Dan'}) | |
.then(data => console.log(data)) // Result from the `response.json()` call | |
function postRequest(url, data) { | |
return fetch(url, { | |
credentials: 'same-origin', // 'include', default: 'omit' | |
method: 'POST', // 'GET', 'PUT', 'DELETE', etc. | |
body: JSON.stringify(data), // Use correct payload (matching 'Content-Type') | |
headers: { 'Content-Type': 'application/json' }, | |
}) | |
.then(response => response.json()) | |
.catch(error => console.error(error)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
من محمد برزگر هستم از ساری
حال شما خوب هست دوست من