iamtesla
iamtesla

Reputation: 423

Request body is always empty on a POST

Client side, this is what I have happening:

 function saveGrades() {
  $.post("/savegrades", {classIndex: "classIndexId"});  }

Server side:

router.post('/savegrades', stormpath.loginRequired, function(req, res)  {
  console.log("Class index: " + req.body.classIndex);
  console.log(req.body);
  res.send(200);
});

My bodyParser settings are as follows:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

No matter what I have tried, req.body is empty and does not have the classIndex. What am I doing incorrectly? Why is the data not being posted to the server?

Edit: For the linked questions, I have gone through almost all relevant answers here and am unable to find a solution. It seems that that data is never being sent to the server whatsoever. The body is always empty when I check it with a debugger.

Upvotes: 2

Views: 781

Answers (2)

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3138

Try this and send data through POSTMEN to verify that data is actually comming then send it through function

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.post('/savegrades', function(req, res)  {
  console.log("Class index: " + req.body.classIndex);
  console.log(req.body);
  res.send(200);
});

Upvotes: 1

Sathik Khan
Sathik Khan

Reputation: 439

Can you please check the code,

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

implemented well before,

router.post('/savegrades',

Based on your comment,

Can you please trying adding Trying adding mime type (content type) in client.

Upvotes: 2

Related Questions