Shopify AJAX API not returning errors in ajax request

when trying to add product to cart using jQuery ajax

like eg:

$.ajax({
  url: '/cart/add',
})

on any error it returns object like:

{
   errors: "Cart Error"
}

but when requesting like

$.ajax({
  url: '/cart/add.js',
})

it correctly returns the errors:

{"status":422,"message":"Cart Error","description":"All 5 Example product are in your cart."}

anybody knows, what shopify is doing?

and they have put “cart/add” on the “{{ routes.cart_add_url }}”

If you look at the docs there is examples of using routes, along with appending the .js endpoint. That’s the url you should be using.

https://shopify.dev/docs/api/ajax/reference/cart

Any reason why you need to do it differently?

okay. because in the themes we store some things in global variable and then use it on js file. dawn does the same as well:


when dawn requests it (using fetch) send the request to “theme.routes.cartAddUrl” which is “/cart/add” not “/cart/add.js” but gets the errors printed.

i can’t figure out why the same route won’t print the error response for ajax request. what does it require from $.ajax?