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

153
Vistas
Ajax sending to server body is undefined

I'm Making a simple voting system using ajax.

here's the code...

// Receving The Votes
app.post('/submitvote', (req, res) => {
    // req.body, body is undefined
    // should be: { choice: 2 }, 2 is just an example
    console.log(req.body);
});

This code saves vote from the judge's into a variable just for testing. But The body(where the choice is) is undefined

let's look at the client side...

var picked = 0

function voteto(plr) {
    +plr;
    picked = plr;
    console.log(`Picked Player #${plr}`)
    if (plr == 1) {
        document.getElementById('plr1').style.backgroundColor = 'blue';
    } else {
        document.getElementById('plr1').style.backgroundColor = 'darkblue';
    }

    if (plr == 2) {
        document.getElementById('plr2').style.backgroundColor = 'blue';
    } else {
        document.getElementById('plr2').style.backgroundColor = 'darkblue';
    }

    if (plr == 3) {
        document.getElementById('plr3').style.backgroundColor = 'blue';
    } else {
        document.getElementById('plr3').style.backgroundColor = 'darkblue';
    }
}

$(document).ready(function(){
    $("#submit").click(function(){
        console.log("Sending Data");
        $.post("/submitvote", {
            choice: picked,
        });
    });
});

The voteto function is a selector for the vote.

I just send the picked variable in the choice parameter(I don't know what would you call that). I didn't make a callback since it's just saving it.

But The Thing Is The Body is undefined. Why?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Add app.use(express.urlencoded()) in the app.js(entry). This Works For Me!

about 4 years ago · Juan Pablo Isaza 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