Axios发布到Node.js服务器,但Node.js无法获取数据 [英] Axios post to nodejs server but nodejs cannot get data
本文介绍了Axios发布到Node.js服务器,但Node.js无法获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将带有axios的vuejs cli发布到nodejs Express服务器:
i post from vuejs cli with axios to nodejs express server:
axios.post('http://localhost:8081/users', bar)
.then((response)=> {
console.log(response)
})
.catch((error)=> {
console.log(error)
})
和服务器:
app.post('/users', (req, res) => {
console.log(req.body.bar)
res.json(req.body.bar)
})
在 http://localhost:8081/users 中,我得到了无法获取/users 代码>和控制台日志窗口:未定义
in http://localhost:8081/users i got Cannot GET /users
and console log windows: undefined
请帮助我!
推荐答案
axios.post('http://localhost:8081/users', {foo: "Bar"})
.then((response)=> {
console.log(response.data) // must be show "Bar"
})
.catch((error)=> {
console.log(error)
})
app.post('/users', (req, res) => {
console.log(req.body.foo) // must be show "Bar"
res.send(req.body.foo)
})
这篇关于Axios发布到Node.js服务器,但Node.js无法获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文