node.js - vue中 post數據遇到問題
問題描述
我在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(); }}
當我請求時后端打出的req.body一直是一個空對象,但是我看了下瀏覽器明明是有post數據過去的
我想問問這是為啥==
問題解答
回答1:問題應該出在你的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或者打印一個數字1看看請求有沒有進去。還可以res.send()一個值看能不能拿到。
相關文章:
1. javascript - JS如何取對稱范圍的隨機數?2. java - ehcache緩存用的是虛擬機內存么?3. 數據庫 - mysql如何處理數據變化中的事務?4. android - java 泛型不支持數組,那么RxJava的Map集合有什么方便的手段可以定義獲得一串共同父類集合數據呢?5. java - mongodb分片集群下,count和聚合統計問題6. 關于docker下的nginx壓力測試7. 服務器端 - 采用nginx做web服務器,C++開發應用程序 出現拒絕連接請求?8. javascript - 有什么兼容性比較好的辦法來判斷瀏覽器窗口的類型?9. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下10. python - pandas按照列A和列B分組,將列C求平均數,怎樣才能生成一個列A,B,C的dataframe
