Vue簡(jiǎn)單封裝axios之解決post請(qǐng)求后端接收不到參數(shù)問(wèn)題
1.在src/下新建api文件夾,api/下新建index.js和public.js
在public.js中:
import axios from 'axios'; import qs from 'qs' import router from '../router' import { MessageBox} from 'mint-ui' // 注意點(diǎn),按照以下寫(xiě) var instance = axios.create(); instance.defaults.timeout = 10000; instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; export default { fetchGet(url, params = {}) { return new Promise((resolve, reject) => { axios.get(url, params).then(res => { if(res.data.code === 302) { MessageBox('提示', '登錄失效,請(qǐng)重新登錄'); MessageBox.alert('登錄失效,請(qǐng)重新登錄', '提示').then(action => { router.push("/login"); }); } resolve(res.data); }).catch(error => { reject(error); }) }) }, fetchPost(url, params = {}) { /* axios post請(qǐng)求后端接收不到參數(shù)問(wèn)題: 解決方案一:有效,但是兼容性不是很好,不是所有瀏覽器都支持 let data = new URLSearchParams() for (var key in params) { data.append(key, params[key]) } */ // 解決方案二:使用qs模塊(axios中自帶),使用qs.stringify()序列化params return new Promise((resolve, reject) => { axios.post(url, qs.stringify(params)).then(res => { resolve(res.data); }).catch(error => { reject(error); }) }) } }
2.在index.js中:
import http from './public' export const getStation = (params) => { return http.fetchGet('/hydro/rest/getBelongUser', params); } export const userLogin = (params) => { return http.fetchPost("/hydro/rest/login", params); }
3.在Login.vue中調(diào)用post請(qǐng)求方法:
<template> <div class="login"> <h1>登錄頁(yè)面</h1> <input type="text" placeholder="請(qǐng)輸入用戶(hù)名" v-model="Username"> <input type="password" placeholder="請(qǐng)輸入密碼" v-model="Password"> <input type="button" value="登錄" @click="toLogin"> </div> </template> <script> import {userLogin} from "../../api/index" export default { name: 'app', data() { return { Username: "", Password: "" } }, methods: { toLogin() { let params = { username: this.Username, password: this.Password }; userLogin(params).then(res => { if(res.code === 200) { this.$router.push("/home") } }) } } } </script>
#### 4.在Home.vue調(diào)用get請(qǐng)求方法
<template> <h1 class="home"> {{stationName}} </h1> </template> <script> import {getStation} from "../../api/index" export default { data() { return{ stationName: "" } }, created() { getStation().then(res => { this.stationName = res.msg; }) } } </script>
總結(jié)
以上所述是小編給大家介紹的Vue簡(jiǎn)單封裝axios之解決post請(qǐng)求后端接收不到參數(shù)問(wèn)題,希望對(duì)大家有所幫助!
相關(guān)文章
基于Vue3實(shí)現(xiàn)鼠標(biāo)滑動(dòng)和滾輪控制的輪播
在這篇文章主要為大家詳細(xì)介紹了如何一步步地實(shí)現(xiàn)一個(gè)基于?Vue?3?的輪播組件,這個(gè)組件的特點(diǎn)是可以通過(guò)鼠標(biāo)滑動(dòng)和滾輪來(lái)控制輪播圖的切換,感興趣的可以了解下2024-02-02淺談vue-lazyload實(shí)現(xiàn)的詳細(xì)過(guò)程
本篇文章主要介紹了淺談vue-lazyload實(shí)現(xiàn)的詳細(xì)過(guò)程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Vue3中的element-plus表格實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue3中的element-plus表格實(shí)現(xiàn)代碼,用組件屬性實(shí)現(xiàn)跳轉(zhuǎn)路由,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05vue3+arco design通過(guò)動(dòng)態(tài)表單方式實(shí)現(xiàn)自定義篩選功能
這篇文章主要介紹了vue3+arco design通過(guò)動(dòng)態(tài)表單方式實(shí)現(xiàn)自定義篩選,本文主要實(shí)現(xiàn)通過(guò)動(dòng)態(tài)表單的方式實(shí)現(xiàn)自定義篩選的功能,用戶(hù)可以自己添加篩選的項(xiàng)目,篩選條件及篩選內(nèi)容,需要的朋友可以參考下2024-05-05vue3+vite使用element-plus問(wèn)題
這篇文章主要介紹了vue3+vite使用element-plus問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10使用vis-timeline繪制甘特圖并實(shí)現(xiàn)時(shí)間軸的中文化(案例代碼)
這篇文章主要介紹了使用vis-timeline繪制甘特圖并實(shí)現(xiàn)時(shí)間軸的中文化(案例代碼),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02vue實(shí)現(xiàn)裁切圖片同時(shí)實(shí)現(xiàn)放大、縮小、旋轉(zhuǎn)功能
這篇文章主要介紹了vue實(shí)現(xiàn)裁切圖片同時(shí)實(shí)現(xiàn)放大、縮小、旋轉(zhuǎn)功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03