简体   繁体   English

POST上的请求正文始终为空

[英]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: 我的bodyParser设置如下:

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 . 不管我尝试了什么, req.body都是空的并且没有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. 当我通过调试器检查主体时,主体始终为空。

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. 您能否尝试添加尝试在客户端中添加mime类型(内容类型)。

Try this and send data through POSTMEN to verify that data is actually comming then send it through function 尝试此操作,然后通过POSTMEN发送数据以验证数据确实在发送,然后通过函数发送

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM