axios post类型请求,express怎么接收?

axios({

  method: 'post',

  url: 'http://localhost:3000/verify',

  headers: {

    'Content-Type': 'application/x-www-form-urlencoded',

    'Accept': 'text/json'

  },

  data: {

    buyParam: JSON.stringify([{ "goodsId": 5977, "amount": 1 }, { "goodsId": 5978, "amount": 1 }])

  }

}).then((res) => {

  console.log(res)

})

router.post('/verify', cors(), function(req, res, next) {

  console.log(req)

  console.log(req.body)

  console.log(req.body.buyParam)

  res.json(req.body.buyParam);

  // res.json({

  //   code: 0,

  //   msg: ''

  // });

});

https://img2.mukewang.com/5c8a0001000188a607550238.jpg

req.body.buyParam // undefined

小唯快跑啊
浏览 749回答 1
1回答

交互式爱情

npm install body-parservar express = require('express')var bodyParser = require('body-parser')var app = express()// parse application/x-www-form-urlencodedapp.use(bodyParser.urlencoded({ extended: false }))// parse application/jsonapp.use(bodyParser.json())app.use(function (req, res) {  res.end(JSON.stringify(req.body, null, 2))})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript