Skip to content

Instantly share code, notes, and snippets.

@hartzis
Created October 6, 2015 01:20
Show Gist options
  • Save hartzis/60d07472f5bf8a913475 to your computer and use it in GitHub Desktop.
Save hartzis/60d07472f5bf8a913475 to your computer and use it in GitHub Desktop.
client side fetch example
function update(data) {
return fetch('/api/update', {
method: 'put',
body: JSON.stringify(data),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(checkStatus)
.then(()=>console.log('updated!!!'))
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response
} else {
var error = new Error(response.statusText)
error.response = response
throw error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment