axios 拦截以及 API简单配置(element)
在某些情况下,有时候会在接口请求的API中需要拿到存在浏览器中的COOKIE,调用的方式可以为:
// 获取浏览器Cookie
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = ; i <ca.length; i++) {
var c = ca[i];
while (c.charAt() == ' ') {
c = c.substring();
}
if (c.indexOf(name) == ) {
return c.substring(name.length, c.length);
}
}
return "";
}
在axios请求中,需要在发送前和发送后进行简单的设置拦截:
// 添加一个响应拦截器
Axios.interceptors.request.use((config) => {
let account = sessionStorage.account;
let token = sessionStorage.token;
// if(config.url == Axios.bullRanking){
// // config.headers['Authorization'] = 'Bearer';
// }else{
// config.headers['Authorization'] = 'Bearer ' + token;
// }
config.headers['Authorization'] = 'Bearer ' + token
// config.headers['Accept'] = 'application/json'
// config.headers['account'] = account;
//在发送请求之前做某件事
// 判断是否登录
// let cur_id = "cur_id",
// sign = "sign";
// if (!cur_id||!sign) {
// localStorage.clear();
// window.location.href = "index.html";
// }; // 给手机验证码接口加mcode
// if(config.url == Axios.send_sms){
// if(sessionStorage.mcode){
// var mcode = sessionStorage.mcode
// config.data.mcode = mcode
// }
// } if(config.url)
if(config.method === 'post'){
config.data = qs.stringify(config.data);
config.data = config.data+"&account=" + account;
config.headers['locale'] = getCookie('language')=='Chinese'?'zh-CN':'en';
config.headers['from'] = 'pc';
}
if(config.method === 'get'){
config.headers['locale'] = getCookie('language')=='Chinese'?'zh-CN':'en';
config.headers['from'] = 'pc';
}
return config;
},(error) =>{
_.toast("错误的传参", 'fail');
return Promise.reject(error);
}); // 添加返回信息验证
Axios.interceptors.response.use(function (response) {
if(response.data.status =='' || response.data.status ==''){
if(!sessionStorage.account){
return false;
}
sessionStorage.clear();
if (process.env.NODE_ENV === 'production') {
window.location.href = "/wap/#/login";
} else {
window.location.href = "/#/login";
} }
// console.log(sessionStorage.account)
if(response.data.status =='' || response.data.code ==''){
Vue.prototype.resetSetItem('watchStorage', 'false');
// sessionStorage.setItem("watchStorage", 'false'); // console.log(123)
if(window.location.hash !='#/home'&&window.location.hash !='#/XXXX'){
Message({
showClose: true,
message: response.data.msg,
type: "error"
});
window.location.href = "#/Login"; return
} }
return response;
}, function (error) {
return Promise.reject(error);
});
其中的
Vue.prototype.resetSetItem('watchStorage', 'false'); 是全局封装的监听sessionStorage变化的。
axios 拦截以及 API简单配置(element)的更多相关文章
- Docker remote API简单配置使用
1.启动docker remote API的方式如下: docker -d -H uninx:///var/run/docker.sock -H tcp://0.0.0.0:5678 2.但是为了伴随 ...
- Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
准备 利用vue-cli脚手架创建项目 进入项目安装vuex.axios(npm install vuex,npm install axios) axios配置 项目中安装axios模块(npm in ...
- Axios拦截器配置
Axios 拦截器的配置如下 分三块:基础配置.请求之前拦截.响应之前拦截 发送所有请求之前和操作服务器响应数据之前对这种情况过滤. http request 请求拦截器 每次发送请求之前判断是否存在 ...
- vue+vux-ui+axios+mock搭建一个简单vue框架
1.首先感谢同事 2.之前一直在做angularjs的项目,目前vue火热,所以自己搭建了一个的vue框架,在此作为记录 vue+vux-ui这里就不介绍了,有很多博客都写的很详细了. 下面简单记录下 ...
- vue请求简单配置
简单记录一下vue的http请求配置相关 测试环境请求接口设置: 1. config/dev.env.js添加: module.exports = merge(prodEnv, { NODE_ENV ...
- Vue2学习小记-给Vue2路由导航钩子和axios拦截器做个封装
1.写在前面 最近在学习Vue2,遇到有些页面请求数据需要用户登录权限.服务器响应不符预期的问题,但是总不能每个页面都做单独处理吧,于是想到axios提供了拦截器这个好东西,再于是就出现了本文. 2. ...
- axios拦截器搭配token使用
在了解到cookie.session.token的作用后学习token的使用 cookie是随着url将参数发送到后台,安全性最低,并且大小受限,不超过4kb左右,它的数据保存在客户端 session ...
- Vue-cli3.x中使用Axios发送跨域请求的配置方法
Vue-cli3.x中使用Axios发送跨域请求的配置方法 安装axios npm i axios -s main.js中引入 import axios from 'axios' //将axios挂载 ...
- 12. 前后端联调 + ( proxy代理 ) + ( axios拦截器 ) + ( css Modules模块化方案 ) + ( css-loader ) + ( 非路由组件如何使用history ) + ( bodyParser,cookieParser中间件 ) + ( utility MD5加密库 ) + ( nodemon自动重启node ) + +
(1) proxy 前端的端口在:localhost:3000后端的端口在:localhost:1234所以要在webpack中配置proxy选项 (proxy是代理的意思) 在package.jso ...
随机推荐
- Mysql数据库在建表时指定engine等于InnoDB 或MyISAM的意义
一.ISAM和InnoDB的定义 1. ISAM ISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数.因此,ISAM执行读取操作的速度很快 ...
- 在Linux环境下部署MySql服务
之前有下载部署过几次,但是每次都会踩一些坑.特此记录在liunx下部署安装mysql的基本步骤: 1.卸载老版本的mysql find / -name mysql|xargs rm -rf 查 ...
- shell中通过读取输入yes no判断下一步如何处理
if [ -d $r_item_rmgit ];then read -p "$r_item_rmgit exit, replace it ...
- 微信小程序开发-入门到熟练(wepy-初级篇)
Title:最近做完了项目,review代码的同时,就想写一篇详细的小程序开发经历,记录自己的项目从0到1的过程 Desc : 小程序从0到1,从小白到完成项目,你需要这样做: step1: 基础知识 ...
- Vue-搭建环境
项目开发完react-native,因为又对vue开始感兴趣了,又开始自学起了vue,关于vue是一个很简便的前端框架,要学习它,当然是要先学会搭建vue的环境, 不会搭建环境的程序员不是一个好的程序 ...
- 【C】题解 (五校联考3day2)
分析 这道题看上去很恶心,实际上只用记录四坨东西就能打DP了:y坐标最小的向上射的点.y坐标最大的向下射的点.y坐标最大和最小的向右射的点,转移显然.注意,如果该状态的值为零就可以略过,否则会超时. ...
- element和iView初步研究(-)
element 1.可以通过npm 和使用cdn 2,支持多种语言组件 3.基本组件还是可以的 iView iView 是一套基于 Vue.js 的开源 UI 组件库,主要服务于 PC 界面的中后台产 ...
- 理解厂商前缀 -webkit- / -moz- / -ms- / -o-
CSS3规范如果想要达到W3C的推荐标准状态还需要不断改进.浏览器则通常在W3C开发标准的过程中就会体现这些特性.这样,标准在最终敲定之前就能知道哪些地方还能进一步改进. 在包含某个特性的的初始阶段, ...
- React Native 之createDrawerNavigator和createSwitchNavigator
其他代码接上篇文章 createDrawerNavigator 抽屉 createSwitchNavigator 模拟登录=>主界面 index.js /** * @format */ impo ...
- RMI实现方案