Node.js - Post data encounter problems in vue
我想大声告诉你
我想大声告诉你 2017-06-05 11:13:07
0
2
781

I wrote the post interface in dev-server.js in vue-cli

app.use(bodyParser.urlencoded({ extended: true })); var apiRouters = express.Router(); // 写几个接口 apiRouters.post('/login', function (req, res) { console.log(req.body); }) app.use('/api', apiRouters);

Then use axios request in vue component

methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { alert('submit!'); let loginParams = { username: this.ruleForm.account, password: this.ruleForm.checkPass }; this.axios.post('/api/login',loginParams).then(response => { console.log(response); }) } else { console.log('error submit!!'); return false; } }); }, resetForm(formName) { console.log('reset'); this.$refs[formName].resetFields(); } }

When I made a request, the req.body typed by the backend was always an empty object, but I checked the browser and it was clear that there was post data.

I want to ask why this is ==

我想大声告诉你
我想大声告诉你

reply all (2)
为情所困

The problem should be in yourdev-server.js. You are missing the correct handling ofrequestBody. Change it to this:

app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); var apiRouters = express.Router(); // 写几个接口 apiRouters.post('/login', function (req, res) { console.log(req.body); }) app.use('/api', apiRouters);

Try again

    刘奇

    You can try printing req or printing a number 1 to see if the request has gone in. You can also res.send() a value to see if you can get it.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!