【发布时间】:2020-11-18 15:26:40
【问题描述】:
我尝试使用 axios 将数据从我的反应应用程序发送到我的节点服务器,但我后端的数据未定义 这是我在服务器上的代码,我想记录发送到服务器的数据
router.post("/register",(req,res) => {
console.log(req.query)
}
这是我在提交时触发的 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));
}
在终端的后端,我得到一组空的花括号
【问题讨论】:
-
你能在
this.stateobj 发送到后面之前发布它吗?你也试过req.body而不是query吗? -
使用
body将数据发送到后端
标签: node.js express axios fetch-api