2

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.

iamtesla
  • 423
  • 2
  • 5
  • 18
  • http://stackoverflow.com/questions/5570747/jquery-posting-json – Andrey Popov Feb 20 '16 at 07:22
  • @AndreyPopov I was unable to resolve my issue with the answers in that question. – iamtesla Feb 20 '16 at 07:27
  • I will just try to link one more :) Have you seen this one (esp. Olli's response)? http://stackoverflow.com/questions/5529685/post-doesnt-send-data-as-json-but-as-x-www-form-urlencoded-instead – Antenka Feb 20 '16 at 07:36
  • which framework are u using express or restify ? – Wasiq Muhammad Feb 20 '16 at 07:39
  • @WasiqMuhammad express – iamtesla Feb 20 '16 at 07:40
  • @Antenka I have changed the code to use `$.ajax` with the settings Olli mentioned and I am still having the same situation occur. – iamtesla Feb 20 '16 at 07:43
  • Is it works without "stormpath.loginRequired" middleware? – Antenka Feb 20 '16 at 07:47
  • @Antenka ended up having to specify the content type in the `$.post` call, no idea why it wasn't working correctly when using AJAX. Ah well, it is working now! The middleware was not an issue. – iamtesla Feb 20 '16 at 07:49
  • 1
    Just found an interesting article (check the "Preflighted requests" section). Looks like an explanation to your problem :) https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – Antenka Feb 20 '16 at 08:05

2 Answers2

2

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.

Sathik Khan
  • 439
  • 4
  • 13
  • It is. My `app.js` file contains the bodyparser and other configuration things. The last line I have in `app.js` is `app.use('/', db)` Where db is my router (where the server code above is located). – iamtesla Feb 20 '16 at 07:28
  • @Shathik thank you! It is now correctly working. The client side code is now: `$.post("/savegrades', {classIndex: "classIndexId"}, 'json');` – iamtesla Feb 20 '16 at 07:48
1

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);
});
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29