Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
Can't read data in node.js from a Ajax POST request

I need help with a very simple problem, but which for some reason I can't solve. I have a ajax post request that send data to a node.js server. From the server side, data seems to be received, but I am not able to retrieve the data fields.

Extract from the Client side:

function validateUser() 
{   
      $.ajax({
      type: "POST",
      url: "/authAjax",
      data: {
        username: "myusr",
        password: "mypsw"
      },
      success: function( result ) {alert("OK");},
      error: function( result ) {alert("ERR");}
    });
}

Extract from the Server side (node.js):

app.post('/authAjax', function(req, res)
{
    req.on('data', function (chunk) {
        console.log(req.body);      // not working
        console.log(req.username);  // not working
        console.log(req.data);      // not working
        res.send("200");            
    });
});

The res.send("200"); is executed, but I have no chance to get the passed username and password values. Could someone give any hint here?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

These were missing on the node.js side:

 app.use(express.json());

And the data is accessible with:

 req.body
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda