Skip to content

Instantly share code, notes, and snippets.

@jeromecornet
Created February 28, 2013 22:40
Show Gist options
  • Save jeromecornet/5060765 to your computer and use it in GitHub Desktop.
Save jeromecornet/5060765 to your computer and use it in GitHub Desktop.
cross domain cart
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function getCart() {
$.ajax({
type: "GET",
url: 'http://restore.myshopify.com/cart.json',
dataType: 'jsonp',
origin: 'http://restore.myshopify.com',
dataFilter: function( data, type ) { console.log(data) },
success: function(data, textstatus, jqXHR){
console.log("got cart")
console.log(data)
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Got error "+errorThrown.toLocaleString())}
})
}
function addToCart() {
// Add the iframe with a unique name
var iframe = document.createElement("iframe");
var uniqueString = "CHANGE_THIS_TO_SOME_UNIQUE_STRING";
document.body.appendChild(iframe);
iframe.style.display = "none";
iframe.contentWindow.name = uniqueString;
// construct a form with hidden inputs, targeting the iframe
var form = document.createElement("form");
form.target = uniqueString;
form.action = "http://restore.myshopify.com/cart/add";
form.method = "POST";
// repeat for each parameter
var input = document.createElement("input");
input.type = "hidden";
input.name = "id";
input.value = "254751118";
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
</script>
</head>
<body>
test:
<div id="content"> </div>
<button onclick="javascript:addToCart()">Add To Cart</button>
<button onclick="javascript:getCart()">Get Cart</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment