Axios发布到nodejs服务器但nodejs无法获取数据

问题描述 投票:0回答:1

我从带有axios的vuejs cli发布到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我得到了Cannot GET /users和控制台日志窗口:undefined

请帮我!

express vue.js axios
1个回答
0
投票
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)
})
© www.soinside.com 2019 - 2024. All rights reserved.