【问题标题】:Getting axios data from post request in my node-js back-end从我的 node-js 后端的 post 请求中获取 axios 数据
【发布时间】:2018-03-13 15:50:13
【问题描述】:

我在我的 vue 组件文件中发送一个 post 请求:

axios.post('/add-couple', {
    word_key: this.word_key,
    word_value: this.word_value
  })
  .then((response) => {
    this.word_key = ''
    this.word_value = ''
  })

并在 dev-server.js 中处理(使用 express):

app.post('/add-couple', (req,res) => {
  newCouple(req.word_key,req.word_value)
  console.log(req.word_key,req.word_value) //undefined undefined 
  res.end()
})

所以,我想使用 word_key 和 word_value 变量,但不能,因为它们都是未定义的。我做错了什么?

【问题讨论】:

  • 尝试使用req.body.word_key

标签: node.js express axios


【解决方案1】:

您应该使用body-parser 中间件和req.boby 对象来获取发送参数:

var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/add-couple', (req, res) => {
  console.log(req.body.word_key, req.body.word_value);
  ...
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    相关资源
    最近更新 更多