[英]Unable to send payload data to the backend in Axios Express
I have tried the to send data from my react application to my node server using axios but I the data at the backend is undefined This is my code at the server where I want to log the data sent to the server我已经尝试使用 axios 将我的反应应用程序中的数据发送到我的节点服务器,但我后端的数据未定义这是我在服务器上的代码,我想在其中记录发送到服务器的数据
router.post("/register",(req,res) => {
console.log(req.query)
}
This is my axios code which is triggered onSubmit这是我在提交时触发的 axios 代码
onSubmit(e){
e.preventDefault();
var newUser = {
name : this.state.name,
email : this.state.email,
password : this.state.password,
password2 : this.state.password2
};
console.log(newUser);
axios.post("/api/users/register",{
headers: {"Access-Control-Allow-Origin": "*",
'Content-Type': 'application/json'},
params : newUser
})
.then(res => console.log(res))
.catch(err => console.log(err));
}
At the backend in the terminal I get empty set of curly braces在终端的后端,我得到一组空的花括号
Post requests are received over req.body
通过req.body
接收发布请求
POST: req.body
发布: req.body
GET: req.query
or req.params
GET: req.query
或req.params
Example req.query
示例req.query
/register?user=1234
Server side:服务器端:
router.get("/register", ...); req.query.user // 1234
Example req.params
示例req.params
/register/1234
Server side:服务器端:
router.get("/register/:user", ...); req.params.user // 1234
You also need an middleware for POST that parses your incoming body.您还需要一个用于解析传入正文的 POST 中间件。
app.use(express.json());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.