Blocked by CORS policy on Ajax

carat
New Member
4 0 0

Hi Team,

I am trying to add the product outside the Shopify with ajax as follows -

 $.ajax({url: "https://storename.myshopify.com/cart/add?id={varient_id}", success: function(result){
}});

 

But I am getting a "has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource" error.

I am listing the product outside Shopify and then with the ajax I want to add the product to the respective user's cart.

With the permalink (ex -https://storename.myshopify.com/cart/add?id={varient_id} ) it is working but the page is redirecting and I don't want page redirection/reload hence use of ajax call it's giving the above error.

 

Is there any solution to this?

Reply 1 (1)
Nitin_Kujur
Shopify Partner
81 19 32

Hello @carat, Below snippet worked for me for this case

$.ajax({ 
      dataType:'jsonp', 
      url: $shop_url + '/cart/add.json?quantity=' + $quantity +'&id=' + $variant_id +'&callback=?', 
      success: function(data){ 
        /* do something with the data. */
        //console.log(data.responseJSON);
        
      } 
});

 

Note:  $shop_url, $quantity and $variant_id are variables.