0

I'm creating a simple API request where I post the basket information of a sale on my woocommerce site to my API. Unfortunately, the body of the api call keeps being empty. I'm clearly missing something really stupid.

var saleObjects = []

//Get the product names
document.querySelectorAll("td.product-name").forEach(function(item){
    var saleItem = {};
    saleItem.title = item.innerText;
    saleObjects.push(saleItem);
});
        
//Get the product prices
document.querySelectorAll("td.product-total").forEach(function(item, i) {
    saleObjects[i].price = item.innerText
    saleObjects[i].commission = 0.05
    saleObjects[i].url = window.location.hostname
});

fetch(API_URL,{
    method: "POST",
    mode: 'no-cors',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({saleObjects: saleObjects})
    }
)
.then(function(response) {
    return response.json();
})
.then(function(jsonResponse) {
    //console.log(jsonResponse)
});

On my backend I'm doing something like this:

console.log("body");
console.log(req.body);
console.log("saleObjects")
console.log(req.body.saleObjects);

var saleObjects = []

if (req.body.saleObjects == undefined) {
    return res.status(401).send();
}

I've been going over this several times with no avail: Fetch: POST JSON data

Am I doing something totally stupid?

0 Answers0