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

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

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 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!