[英]Not able to get data (node.js) from my client (react.js)
I'm not able to retrieve the data I'm sending from the client to the server.我无法检索从客户端发送到服务器的数据。
My react code is the following:我的反应代码如下:
axios.post('http://localhost:5000/post-entry', { data: formData })
.then(res => {
console.log(res);
})
My server code is the following:我的服务器代码如下:
app.post('/post-entry', (req, res) => {
console.log(req.data, "res.body.data here");
});
When it reaches the post on my server, the log is undefined
.当它到达我服务器上的帖子时,日志是
undefined
。
What am I doing wrong?我究竟做错了什么?
If formData
is a JS object, just axios.post('http://localhost:5000/post-entry', formData)
is enough.如果
formData
是 JS 对象,只需axios.post('http://localhost:5000/post-entry', formData)
就足够了。 You can access this object on server side by req.body
.您可以通过
req.body
在服务器端访问此对象。 But ExpressJS needs a middleware to parse request bodies.但是 ExpressJS 需要一个中间件来解析请求体。 You have to use
app.use(express.json())
or app.use(bodyParser.json())
where bodyParser is body-parser
.您必须使用
app.use(express.json())
或app.use(bodyParser.json())
其中 bodyParser 是body-parser
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.