node.js - vue中 post數(shù)據(jù)遇到問題
問題描述
我在vue-cli中的dev-server.js中寫了post的接口
app.use(bodyParser.urlencoded({ extended: true }));var apiRouters = express.Router();// 寫幾個接口apiRouters.post(’/login’, function (req, res) { console.log(req.body);})app.use(’/api’, apiRouters);
然后在vue組件中用axios請求
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(); }}
當(dāng)我請求時后端打出的req.body一直是一個空對象,但是我看了下瀏覽器明明是有post數(shù)據(jù)過去的
我想問問這是為啥==
問題解答
回答1:問題應(yīng)該出在你的dev-server.js里,你缺了對requestBody的正確處理,改成這樣:
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);
再試一次
回答2:你可以試試打印req或者打印一個數(shù)字1看看請求有沒有進去。還可以res.send()一個值看能不能拿到。
相關(guān)文章:
1. css3 - less或者scss 顏色計算的知識應(yīng)該怎么學(xué)?或者在哪里學(xué)?2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?3. Span標(biāo)簽4. 關(guān)于Mysql數(shù)據(jù)表行轉(zhuǎn)列5. extra沒有加載出來6. win10系統(tǒng) php安裝swoole擴展7. php - 第三方支付平臺在很短時間內(nèi)多次異步通知,訂單多次確認(rèn)收款8. django進行數(shù)據(jù)庫的查詢9. mysql - 為什么where條件中or加索引不起作用?10. PHP求助,求幫忙謝謝各位
