最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決vue項(xiàng)目axios每次請(qǐng)求session不一致的問題

 更新時(shí)間:2020年10月24日 09:39:08   作者:lilongwei4321  
這篇文章主要介紹了解決vue項(xiàng)目axios每次請(qǐng)求session不一致的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

1、vue開發(fā)后臺(tái)管理項(xiàng)目,登錄后,請(qǐng)求數(shù)據(jù)每次session都不一致,后臺(tái)返回未登錄,處理方法打開main.js設(shè)置:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
require('es6-promise').polyfill()
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from './store'
import axios from 'axios' // 1、在這里引入axios 
 
axios.interceptors.response.use(function(res) {
 var res = res.data;
 if(res.status === 403 ) {
 router.push('/')
 return res;
 }
 return res;
}, function(error) {
 return Promise.reject(error);
});
axios.defaults.withCredentials = true; //意思是攜帶cookie信息,保持session的一致性
Vue.prototype.$axios = axios
Vue.prototype.stringify = require('qs').stringify; 
 
Vue.use(MintUI)
Vue.use(ElementUI);
Vue.config.productionTip = false
 
/* eslint-disable no-new */
new Vue({
 el: '#app',
 router,
 store,
 components: { App },
 template: '<App/>'
})

withCredentials為false意思是不攜帶cookie信息,為保持session的一致性需設(shè)置為true;

2、為解決跨域,需要代理

3、數(shù)據(jù)請(qǐng)求

補(bǔ)充知識(shí):解決跨域造成Vue-element每次請(qǐng)求sessionID不同問題

vue-element作為前端開發(fā)框架, 前后端分離項(xiàng)目ajax跨域, 每次http請(qǐng)求后sessionId均會(huì)發(fā)生變化,導(dǎo)致獲取session失敗,

只需要在文件vue-element-admin-master-1\src\utils\request.js中添加如下代碼即可:

withCredentials: true,

crossDomain: true

整個(gè)axios請(qǐng)求為:

const service = axios.create({
 baseURL: process.env.BASE_API, // api的base_url
 timeout: 5000, // request timeout
 withCredentials: true,
 crossDomain: true
})

以上這篇解決vue項(xiàng)目axios每次請(qǐng)求session不一致的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

正镶白旗| 池州市| 富源县| 庆元县| 上高县| 长沙县| 洛宁县| 洪洞县| 时尚| 惠水县| 亳州市| 东台市| 克东县| 宝鸡市| 福州市| 两当县| 德兴市| 同江市| 延安市| 休宁县| 包头市| 股票| 汉川市| 枞阳县| 陇川县| 玉山县| 宁夏| 卢氏县| 南通市| 平谷区| 万山特区| 古田县| 堆龙德庆县| 二手房| 扶绥县| 化隆| 兴宁市| 河北区| 罗源县| 宁乡县| 尼木县|