无法从我的客户端 (react.js) 获取数据 (node.js)
Not able to get data (node.js) from my client (react.js)
我无法检索从客户端发送到服务器的数据。
我的反应代码如下:
axios.post('http://localhost:5000/post-entry', { data: formData })
.then(res => {
console.log(res);
})
我的服务器代码如下:
app.post('/post-entry', (req, res) => {
console.log(req.data, "res.body.data here");
});
当它到达我服务器上的 post 时,日志是 undefined
。
我做错了什么?
如果formData
是JS对象,那么axios.post('http://localhost:5000/post-entry', formData)
就够了。
您可以通过 req.body
在服务器端访问此对象。但是 ExpressJS 需要一个中间件来解析请求体。您必须使用 app.use(express.json())
或 app.use(bodyParser.json())
,其中 bodyParser 是 body-parser
.
我无法检索从客户端发送到服务器的数据。
我的反应代码如下:
axios.post('http://localhost:5000/post-entry', { data: formData })
.then(res => {
console.log(res);
})
我的服务器代码如下:
app.post('/post-entry', (req, res) => {
console.log(req.data, "res.body.data here");
});
当它到达我服务器上的 post 时,日志是 undefined
。
我做错了什么?
如果formData
是JS对象,那么axios.post('http://localhost:5000/post-entry', formData)
就够了。
您可以通过 req.body
在服务器端访问此对象。但是 ExpressJS 需要一个中间件来解析请求体。您必须使用 app.use(express.json())
或 app.use(bodyParser.json())
,其中 bodyParser 是 body-parser
.