node.js - NodeJS req.body 始终为空
问题描述
我的 server.js 代码:
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
const AboutController = require('./controllers/AboutController');
app.use('/about', AboutController);
关于Controller.js
router.post('/store', (req, res, next) => {
// GETTING REQUEST DETAILS AND INITIALIZATION
let content = req.body.content;
let company_name = req.body.company_name;
console.log(req.body);
});
module.exports = router;
问题
req.body总是返回{}
空对象,我不知道为什么!
我试过的
我试过了console.log(req)
,它返回了对象,但对象仍然body
是空的!
客户端请求
我正在使用 Postman表单数据来模拟客户端请求。
解决方案
我找到了解决方案..
我使用的是 Postman,我使用的是表单数据而不是raw和json,它工作得很好,我还不知道为什么!
推荐阅读
- python - 如何在 Scikit Learn 中判断神经网络是否过拟合
- mongodb - Mongodb聚合将集合中的字段附加到另一个集合中数组中的每个文档
- powerbi - 表格视觉限制行
- python - 如何在电子应用程序和打开的其他浏览器之间进行通信?
- javascript - ReactJS ProgressBar 值未与 state.progressValue 同步更新
- typescript - Sequelize Typescript on delete cascade throwing errors
- cloudflare - 使用 cloudflare 将请求路由到不同的 Web 应用程序
- java - @Autowired vs @Autowired with Setter
- node.js - Angular 6 - 删除 node_module 包并将其用于本地项目更改
- python-2.7 - 为什么由 PIL draw.text() 图像制成的数组不能在 Matplotlib 中正确显示?