Cart/add.js not working with custom domain

Hi all,

Cart/add.js is not working with the custom domain. As usual, CORS error. what to do?

Thanks in advance.

Hi @Girish_Rajwani,

AJAX API calls should be made to Locale-aware URLs, as mentioned in the documentation.

The locale aware URLs can be built using either the routes liquid object, or in Javascript with the window.Shopify.routes.root object

For example with the Liquid Object, you can add a product to the cart with the routes.cart_add_url property

Or with a fetch method in Javascript:

fetch(window.Shopify.routes.root + 'cart/add.js', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(formData)
})
.then(response => {
  return response.json();
})
.catch((error) => {
  console.error('Error:', error);
});

Using either of these methods above should prevent the CORS errors from occurring.