Skip to content

Instantly share code, notes, and snippets.

@brettvaida
Created November 26, 2017 00:10
Show Gist options
  • Save brettvaida/4127e2752f5c544803ae759de48b5c5d to your computer and use it in GitHub Desktop.
Save brettvaida/4127e2752f5c544803ae759de48b5c5d to your computer and use it in GitHub Desktop.
AJAX POST request boilerplate using fetch(). May require polyfill from https://github.com/github/fetch
fetch('https://api-to-call.com/endpoint', {
method: 'POST',
body: JSON.stringify({id: '200'})
}).then(response => {
if (response.ok) {
return response.json();
} throw new Error('Request failed!');
}, networkError => console.log(networkError.message)
).then(jsonResponse => jsonResponse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment