• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

135
Vistas
javascript body is empty on api - but not empty when sending the request

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?

almost 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda