国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

node.js - vue中 post數據遇到問題

瀏覽:155日期:2023-09-05 08:06:06

問題描述

我在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數據過去的

node.js - vue中 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()一個值看能不能拿到。

標簽: vue
相關文章:
主站蜘蛛池模板: 雷州市| 昌江| 楚雄市| 深泽县| 南昌县| 读书| 枣庄市| 石河子市| 京山县| 南宁市| 剑川县| 麟游县| 高碑店市| 卢湾区| 喀喇沁旗| 闽侯县| 榆社县| 京山县| 榕江县| 宾川县| 玛沁县| 马尔康县| 库尔勒市| 天津市| 崇义县| 汤原县| 梅河口市| 平定县| 云龙县| 嘉兴市| 两当县| 定州市| 离岛区| 丰顺县| 千阳县| 曲松县| 遂川县| 聂荣县| 青海省| 略阳县| 滕州市|