Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!