Last active
July 1, 2016 10:09
-
-
Save zshell31/79791da5e0eb08f82e743f0e764734be to your computer and use it in GitHub Desktop.
Json Server Api calls
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 api(method, uri, body) { | |
url = 'http://localhost:3000/' + uri; | |
return fetch(url, { | |
method: method, | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(body) | |
}) | |
.then(function(response) { | |
return response.json() | |
}) | |
.then(function(json) { | |
console.log('parsed json: ', json) | |
}) | |
.catch(function(ex) { | |
console.log('parsing failed: ', ex) | |
}); | |
} | |
function get(uri) { return api('GET', uri); } | |
function post(uri, body) { return api('POST', uri, body); } | |
function put(uri, body) { return api('PUT', uri, body); } | |
function del(uri) { return api('DELETE', uri); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment