Created
October 6, 2015 01:20
-
-
Save hartzis/60d07472f5bf8a913475 to your computer and use it in GitHub Desktop.
client side fetch example
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
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