【发布时间】:2019-10-22 22:12:09
【问题描述】:
我正在对我的后端使用一个简单的发布请求来获取表单数据,但由于某种原因,正文总是空的。 我试图隔离这一点,所以我将内容类型更改为应用程序 json 并将数据更改为 json,只有这样我才能发送数据。
客户端:
submitForm(event) {
event.preventDefault();
console.log("gggg");
const data = new FormData(event.target);
axios.post("http://localhost:4000/user-form-post",data).then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
服务器端:
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended:true}));
app.use(express.urlencoded());
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
app.use(logger('dev'));
app.post('/user-form-post', (req,res) =>{
console.log("dfdf");
console.log(req.body); // alwayes print empty dict {}
res.end();
})
这不起作用,因为它需要 jsons(预期行为):
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended:true}));
与 Postman 的行为相同。
【问题讨论】:
-
解析应用程序/x-www-form-urlencoded,你不应该使用
app.use(bodyParser.urlencoded({ extended: false }))吗? -
也试过了。不工作
-
已经看过这个...没有任何帮助
标签: node.js express client backend body-parser