Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

402
Vistas
nodejs req.body undefined in case of ajax post requests

Strange issue and I have no explanation to that. I create ajax post request

 $.ajax({  
        type: "POST",  
        url: "/like",  
        data: {"topic": "123123"},  
        success: function(dataString) {               
            console.log('123123'); 
        }  
    }); 

Or like this:

var xhttp = new XMLHttpRequest(); 
xhttp.open("POST", "/like", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford"); 

on server side (NodeJs) I have simple code

app.post('/like', function (req, res) {
    console.log(req.body);
    res.status(201).send('+1')
});

what I dont understand, is why I receive all the time undefined when I send ajax post request.

enter image description here

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to use a body parser, see: nodejs.dev/learn/get-http-request-body-data-using-nodejs

over 4 years ago · Santiago Trujillo Denunciar
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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda