Skip to content

Instantly share code, notes, and snippets.

@lukeshumard
Created December 1, 2016 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lukeshumard/80718821e2864bb1b46ed7f8127eb74c to your computer and use it in GitHub Desktop.
Save lukeshumard/80718821e2864bb1b46ed7f8127eb74c to your computer and use it in GitHub Desktop.
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
};
function parseJSON(response) {
return response.json();
};
var variant_id = 100;
fetch('/cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: JSON.stringify({"id":variant_id,"quantity":1})
})
.then(checkStatus)
.then(parseJSON)
.then(function(json) {
console.log(json);
})
.catch(function(error) {
console.log('request failed', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment