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. javascript - 從mysql獲取json數(shù)據(jù),前端怎么處理轉(zhuǎn)換解析json類型2. macos - mac下docker如何設(shè)置代理3. apache - 本地搭建wordpress權(quán)限問題4. javascript - 如何獲取未來元素的父元素在頁面中所有相同元素中是第幾個?5. Whitelabel錯誤頁面發(fā)生意外錯誤(類型=未找到,狀態(tài)= 404)/WEB-INF/views/home.jsp6. javascript - 學(xué)習(xí)網(wǎng)頁開發(fā),關(guān)于head區(qū)域一段腳本的疑惑7. Android下,rxJava+retrofit 并發(fā)上傳文件和串行上傳文件的效率為什么差不多?8. 熱切期待朱老師的回復(fù),網(wǎng)頁視頻在線播放器插件配置錯誤9. dockerfile - 為什么docker容器啟動不了?10. angular.js - ng-grid 和tabset一起用時,grid width默認特別小
