从我的 node-js 后端中的 post 请求获取 axios 数据

Getting axios data from post request in my node-js back-end

我在我的 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 变量,但不能,因为它们都是未定义的。我做错了什么?

您应该使用 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);
  ...
});