Javascript 请求后,req.body为空{}
body正在发送数据,但在提交表单时返回{} 这就是我用的中间件Javascript 请求后,req.body为空{},javascript,json,express,body-parser,Javascript,Json,Express,Body Parser,body正在发送数据,但在提交表单时返回{} 这就是我用的中间件 app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false})); 但是bodyParser一词被标记为消息“bodyParser已弃用”。ts(6385), 此处将声明标记为已弃用 我试着用 app.use(express.json()); app.use(express.urlencoded({ extended: false})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
但是bodyParser一词被标记为消息“bodyParser已弃用”。ts(6385),
此处将声明标记为已弃用
我试着用
app.use(express.json());
app.use(express.urlencoded({ extended: false}));
但它仍然在req.body上返回{}
您确定在发送POST请求时使用了正确的标题吗 对于
.json()
解析,标题应包含:内容类型:application/json
对于urlencoded()
表单数据解析,标题应包含:内容类型:application/x-www-form-urlencoded
您还可以共享邮递员请求的卷曲吗?在注册依赖项时,请参阅编码行,如果您注册了express json,则该行应该在路由之前,因此它看起来像:
const express = require('express');
const app = express();
// middleware registered here
app.use(express.json());
// then your route
app.post('/someroute', async (req,res,next) => {
const { param1, param2 } = req.body;
try {
console.log(`${param1} and ${param2} written in terminal);
res.send('ok');
} catch (err) {
res.send('oopps');
}
})
是的,标题包含的内容类型为:application/x-www-form-urlencoded,在主体中我发送键和值,并在控制台中获得结果。但是,当我填写表单并提交时,它将返回{},对象ID保存在数据库中,