Api call always 404s in preview mode

Api call always 404s in preview mode

lolitsjef
Visitor
1 0 0

I am trying to make a "kit builder" for my friends shopify store.

I'm very new to web dev.

 

i basically want to have blocks of products in which a customer can select 1 product form each block. When they select a product i update an array in my script to store the currently selected products. then I have a button that should call the api to add these items to cart. but whenever i try to do this I get a 404 saying: 

 

{"status":"bad_request","message":"Parameter Missing or Invalid","description":"Required parameter missing or invalid: items"}

 

I have simplified everything to the example request in the docs: 

 

 

 

 

 

function checkout(){
    let formData = {
        'items': [{
         'id': 8117069185265,
         'quantity': 1
         }]
       };
    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);
    });
}

 

 

 

 
The product has no variants (i need to add some variant handling but I'm just trying to get the basics working). Am I doing something wrong? I cant seem to figure out why this wouldn't work? I am testing in "preview mode" if that makes any difference. Thank you

 

Replies 0 (0)