vue中如何簡單封裝axios淺析
把axios注入到Vue中
import axios from 'axios'; Vue.prototype.$axios = axios;
import axios from 'axios' axios.defaults.timeout = 5000; //響應(yīng)時間 axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'; //配置請求頭 axios.defaults.withCredentials= true; //帶cookie axios.defaults.baseURL = 'http://localhost:8080/'; //配置接口地址 //POST傳參序列化(添加請求攔截器) axios.interceptors.request.use((config) => { //在這里可以添加序列化代碼,根據(jù)后端而定,我用的SSM后端接受Json對象,假如需要序列化可以用qs組件 return config; },(error) =>{ console.log('錯誤的傳參') return Promise.reject(error); }); //返回狀態(tài)判斷(添加響應(yīng)攔截器) axios.interceptors.response.use((res) =>{ //對響應(yīng)數(shù)據(jù)做些事 if(!res.data.success){ return Promise.resolve(res); } return res; }, (error) => { console.log('網(wǎng)絡(luò)異常') return Promise.reject(error); }); //返回一個Promise(發(fā)送post請求) export function fetchPost(url, params) { return new Promise((resolve, reject) => { axios.post(url, params) .then(response => { resolve(response); }, err => { reject(err); }) .catch((error) => { reject(error) }) }) } 返回一個Promise(發(fā)送get請求) export function fetchGet(url, param) { return new Promise((resolve, reject) => { axios.get(url, {params: param}) .then(response => { resolve(response) }, err => { reject(err) }) .catch((error) => { reject(error) }) }) } export default { fetchPost, fetchGet, }
簡單測試:
loginPost: function() { let params = { 'password': '123', 'username': 'admin' } http.fetchPost('/login', params).then((data) => { console.log(data) }).catch(err => { console.log(err) }) }, hello: function() { http.fetchGet('/hello', "").then((data) => { console.log(data) }).catch(err => { console.log(err) }) },
post請求:
get請求:
跨域問題,這里是在后端配置的:
在SpringMVC.xml中配置跨域:
<!-- 接口跨域配置 --> <mvc:cors> <!-- allowed-methods="*" --> <!-- 表示所有請求都有效 --> <mvc:mapping path="/**" allowed-origins="*" allowed-methods="POST, GET, OPTIONS, DELETE, PUT" allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" allow-credentials="true"/> </mvc:cors><!-- 接口跨域配置 -->
接口配置:
在Vue中配置 創(chuàng)建一個vue.config.js:
module.exports = { devServer: { proxy: { '/api': { target: 'http://127.0.0.1:8080', // 在本地會創(chuàng)建一個虛擬服務(wù)端,然后發(fā)送請求的數(shù)據(jù),并同時接收請求的數(shù)據(jù),這樣服務(wù)端和服務(wù)端進行數(shù)據(jù)的交互就不會有跨域問題 changeOrigin: true, ws: true, pathRewrite: { '^/api': '' // 替換target中的請求地址,也就是說以后你在請求http://api.jisuapi.com/XXXXX這個地址的時候直接寫成/api即可 } } } } }
后端也能正常獲取cookie。
當(dāng)然要注意下面這個配置,這個是帶cookie的原因
axios.defaults.withCredentials= true;
總結(jié)
到此這篇關(guān)于vue中如何簡單封裝axios的文章就介紹到這了,更多相關(guān)vue封裝axios內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue之使用echarts圖表setOption多次很卡問題及解決
這篇文章主要介紹了vue之使用echarts圖表setOption多次很卡問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Vue中JSON文件神奇應(yīng)用fetch、axios異步加載與模塊導(dǎo)入全指南詳細教程
在Vue中使用JSON文件有多種方式,包括使用fetch方法加載JSON文件、使用axios庫加載JSON文件,以及將JSON文件導(dǎo)入為模塊,這篇文章主要介紹了Vue中JSON文件神奇應(yīng)用fetch、axios異步加載與模塊導(dǎo)入全指南詳細教程,需要的朋友可以參考下2024-01-01Vue常用API、高級API的相關(guān)總結(jié)
這篇文章主要介紹了Vue常用API、高級API的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-02-02Vue使用mounted和created時,this無法指向data中的數(shù)據(jù)問題
這篇文章主要介紹了Vue使用mounted和created時,this無法指向data中的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07