vue axios 封装(一)
封装一:
'use strict' import axios from 'axios'
import qs from 'qs'
import NProgress from 'nprogress'
import vm from '../main' axios.interceptors.request.use(config => { //利用拦截器做预处理
// 这里可以加一些动作, 比如来个进度条开始动作,
NProgress.start()
return config
}, error => {
return Promise.reject(error)
}) axios.interceptors.response.use(response => response, error => Promise.resolve(error.response)) // 这里我们把错误信息扶正, 后面就不需要写 catch 了 //处理来自网络或者服务器的错误
function checkStatus(response) {
NProgress.done() // if (response.status == 666) {
// localStorage.clear();
// this.$router.push('/')
// }
// 如果 http 状态码正常, 则直接返回数据
// 200请求成功 304浏览器缓存
if (response.status === 200 || response.status === 304 || response.status == 400) {
return response
}
if (response.status == 401) {
console.log("token错误");
vm.$router.push("/");
return response
}
// 异常状态下, 把错误信息返回去
// 因为前面我们把错误扶正了, 不然像 404, 500 这样的错误是走不到这里的
return {
data: {
code: -404,
message: response.statusText,
data: response.statusText,
}
}
}
//处理来自程序端的错误,
function checkCode(res) { // 如果状态 code 异常(这里已经包括网络错误, 服务器错误, 后端抛出的错误), 可以弹出一个错误提示, 告诉用户
if ((res.data.code || res.data.Code) && res.data.code !== 1000) {
//登录超时--重新登录
if (res.data.code === 1100) {
//alert("会话过期...,请重新登录...")
vm.$router.push('/')
} else if (res.data.Code === 1500) {
vm.$message.error({
message: res.data.Error,
type: "error"
});
} else {
vm.$message.error({ message: '服务器开小差了,请稍后再试', type: "error" });
}
}
return res;
} export default {
post(url, data) {
const token = JSON.parse(localStorage.getItem("token")) return axios({
method: 'post', //请求协议
url, //请求地址
data: qs.stringify(data), //请求的数据
timeout: 60000, //超时时间---单位是毫秒
headers: {
'Authorization': token ? token.token_type + ' ' + token.access_token : '',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'AJAX_FLAG': ' TRUE'
} //请求头
}).then(checkStatus).then(checkCode)
},
postList(url, data) {
const token = JSON.parse(localStorage.getItem("token"));
return axios({
method: 'post', //请求协议
url, //请求地址
data: data, //请求的数据
timeout: 60000, //超时时间---单位是毫秒
headers: {
'Authorization': token ? token.token_type + ' ' + token.access_token : '',
'X-Requested-With': 'XMLHttpRequest',
// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'AJAX_FLAG': ' TRUE'
} //请求头
}).then(checkStatus).then(checkCode)
},
get(url, params) {
const token = JSON.parse(localStorage.getItem("token"));
return axios({
method: 'get', //请求协议
url, //请求地址
params, //请求的数据
timeout: 60000, //超时时间---单位是毫秒
headers: {
'Authorization': token ? token.token_type + ' ' + token.access_token : '',
'X-Requested-With': 'XMLHttpRequest',
'AJAX_FLAG': ' TRUE'
} //请求头
}).then(checkStatus).then(checkCode)
},
delete(url, params) {
const token = JSON.parse(localStorage.getItem("token"));
return axios({
method: 'delete', //请求协议
url, //请求地址
params, //请求的数据
timeout: 60000, //超时时间---单位是毫秒
headers: {
'Authorization': token ? token.token_type + ' ' + token.access_token : '',
'X-Requested-With': 'XMLHttpRequest',
'AJAX_FLAG': ' TRUE'
} //请求头
}).then(checkStatus).then(checkCode)
},
put(url, data) {
const token = JSON.parse(localStorage.getItem("token"))
return axios({
method: 'put', //请求协议
url, //请求地址
data: qs.stringify(data), //请求的数据
timeout: 60000, //超时时间---单位是毫秒
headers: {
'Authorization': token ? token.token_type + ' ' + token.access_token : '',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'AJAX_FLAG': ' TRUE'
} //请求头
}).then(checkStatus).then(checkCode)
},
putList(url, data) {
const token = JSON.parse(localStorage.getItem("token"))
return axios({
method: 'put', //请求协议
url, //请求地址
data: data, //请求的数据
timeout: 60000, //超时时间---单位是毫秒
headers: {
'Authorization': token ? token.token_type + ' ' + token.access_token : '',
'X-Requested-With': 'XMLHttpRequest',
// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'AJAX_FLAG': ' TRUE'
} //请求头
}).then(checkStatus).then(checkCode)
},
}
vue axios 封装(一)的更多相关文章
- vue axios封装以及登录token过期跳转问题
Axios配置JWT/封装插件/发送表单数据 首先请务必已仔细阅读 Axios 文档并熟悉 JWT: 中文文档 JWT 中文文档 安装 npm install axios npm install es ...
- vue axios 封装(二)
封装二: http.js import axios from 'axios' import storeHelper from './localstorageHelper' // 全局设置 const ...
- vue Axios 封装与配置项
import axios from "axios"; import qs from "qs"; import { Message } from "el ...
- vue(axios)封装,content-type由application/json转换为application/x-www-form-urlencoded
现在主流的http请求头的content-type有三种(不讨论xml): application/x-www-form-urlencoded 最常见的提交数据方式,与原生form表单数据一致,在c ...
- vue axios封装
前言: 对第三方库进行二次封装和抽离到统一模块,项目面对自己的模块进行开发.如果有一天更换库,只需要修改自己模块中的代码,无需对整个项目进行重构. 将axios网络请求库封装到network文件下的r ...
- Vue axios封装 实现请求响应拦截
封装 axios.js import axios from 'axios' import { baseURL } from '@/config' class HttpRequest { constru ...
- vue axios 封装(三)
封装三: import axios from 'axios' import { Message, MessageBox } from 'element-ui' import store from '. ...
- vue axios封装以及API统一管理
在vue项目中,每次和后台交互的时候,经常用到的就是axios请求数据,它是基于promise的http库,可运行在浏览器端和node.js中.当项目越来越大的时候,接口的请求也会越来越多,怎么去管理 ...
- 把axios封装为vue插件使用
前言 自从Vue2.0推荐大家使用 axios 开始,axios 被越来越多的人所了解.使用axios发起一个请求对大家来说是比较简单的事情,但是axios没有进行封装复用,项目越来越大,引起的代码冗 ...
随机推荐
- Edusoho之LAMP环境搭建
主要参考官方文档Ubuntu16.04+Apache+PHP+MySQL+EduSoho 安装教程LAMP环境按照如下搭建是没有问题的,本地虚拟机试验是完全没有问题的. 1.更新 sudo apt-g ...
- 从String.valueOf(null)说起
同学在群问String.valueOf(null)返回啥,我看了下源码,返回"null"啊, public static String valueOf(Object obj) ...
- Django model中的class Meta详解
通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...
- 腾讯AlloyTeam正式发布omi-cli脚手架 - 创建网站无需任何配置
omi-cli omi-cli omi-cli命令 omi框架 用户指南 文件目录 npm 脚本 npm start npm run dist 代码分割 兼容 IE8 插入 CSS 插入组件局部 CS ...
- nodejs简单模仿web.net web api
最近用了asp.net web api + EF开发一个项目,但是移植到linux时遇到问题(mono只支持EF6.0,但是mysql驱动不支持EF6.0).所以决定换个思路,用nodejs实现res ...
- elasticsearch简单操作(一)
1.增加记录 例如1:向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录.比如,向/accounts/person发送请求,就可以新增一条人员记录. curl ...
- H5 类选择器
10-类选择器 错误的写法: --> 迟到毁一生 早退穷三代 按时上下班 必成高富帅 我是段落 我是段落 <!DOCTYPE html> <html lang="en ...
- 【M2】软件工程终期总结报告——前端设计总结
PhylabWeb——前端设计感想 简介 本文的内容是关于我参与的软件工程项目——“Phylab-Web物理实验中心网站”的前端设计个人总结,来自团队:软剑攻城队 网站地址为:http://buaap ...
- Day15 Python基础之logging模块(十三)
参考源:http://www.cnblogs.com/yuanchenqi/articles/5732581.html logging模块 (****重点***) 一 (简单应用) import lo ...
- iOS使用XZMRefresh实现UITableView或UICollectionView横向刷新
https://blog.csdn.net/u013285730/article/details/50615551?utm_source=blogxgwz6 XZMRefresh The easies ...