封装一:

'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 封装(一)的更多相关文章

  1. vue axios封装以及登录token过期跳转问题

    Axios配置JWT/封装插件/发送表单数据 首先请务必已仔细阅读 Axios 文档并熟悉 JWT: 中文文档 JWT 中文文档 安装 npm install axios npm install es ...

  2. vue axios 封装(二)

    封装二: http.js import axios from 'axios' import storeHelper from './localstorageHelper' // 全局设置 const ...

  3. vue Axios 封装与配置项

    import axios from "axios"; import qs from "qs"; import { Message } from "el ...

  4. vue(axios)封装,content-type由application/json转换为application/x-www-form-urlencoded

    现在主流的http请求头的content-type有三种(不讨论xml): application/x-www-form-urlencoded  最常见的提交数据方式,与原生form表单数据一致,在c ...

  5. vue axios封装

    前言: 对第三方库进行二次封装和抽离到统一模块,项目面对自己的模块进行开发.如果有一天更换库,只需要修改自己模块中的代码,无需对整个项目进行重构. 将axios网络请求库封装到network文件下的r ...

  6. Vue axios封装 实现请求响应拦截

    封装 axios.js import axios from 'axios' import { baseURL } from '@/config' class HttpRequest { constru ...

  7. vue axios 封装(三)

    封装三: import axios from 'axios' import { Message, MessageBox } from 'element-ui' import store from '. ...

  8. vue axios封装以及API统一管理

    在vue项目中,每次和后台交互的时候,经常用到的就是axios请求数据,它是基于promise的http库,可运行在浏览器端和node.js中.当项目越来越大的时候,接口的请求也会越来越多,怎么去管理 ...

  9. 把axios封装为vue插件使用

    前言 自从Vue2.0推荐大家使用 axios 开始,axios 被越来越多的人所了解.使用axios发起一个请求对大家来说是比较简单的事情,但是axios没有进行封装复用,项目越来越大,引起的代码冗 ...

随机推荐

  1. copy from insert using 语句迁移数据

    使用copy实现long类型转移表空间,表空间的数据文件损坏,在转移该表空间相关表时,遇到让人郁闷的long类型.不能使用ctas和move来实现转移,最后通过古老的copy来实现该项工作. 1.模拟 ...

  2. CF960G Bandit Blues 第一类斯特林数、NTT、分治/倍增

    传送门 弱化版:FJOI2016 建筑师 由上面一题得到我们需要求的是\(\begin{bmatrix} N - 1 \\ A + B - 2 \end{bmatrix} \times \binom ...

  3. 高并发下的Java数据结构(List、Set、Map、Queue)

    由于并行程序与串行程序的不同特点,适用于串行程序的一些数据结构可能无法直接在并发环境下正常工作,这是因为这些数据结构不是线程安全的.本节将着重介绍一些可以用于多线程环境的数据结构,如并发List.并发 ...

  4. Rollup处理并打包JS文件项目实例

    关于Rollup rollup是一款用来es6模块打包代码的构建工具(支持css和js打包).当我们使用ES6模块编写应用或者库时,它可以打包成一个单独文件提供浏览器和Node.js来使用. 它的优点 ...

  5. 朱晔的互联网架构实践心得S2E5:浅谈四种API设计风格(RPC、REST、GraphQL、服务端驱动)

    Web API设计其实是一个挺重要的设计话题,许多公司都会有公司层面的Web API设计规范,几乎所有的项目在详细设计阶段都会进行API设计,项目开发后都会有一份API文档供测试和联调.本文尝试根据自 ...

  6. .NET Core 中正确使用 HttpClient 的姿势

    为了更方便在服务端调用 HTTP 请求,微软在 .NET Framework 4.x 的时候引入了 HttpClient.但 HttpClient 有很多严重问题,一直饱受诟病,比如 InfoQ 的这 ...

  7. H5 54-清空默认边距

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. HDU - 1255 扫描线+离散化进阶

    这道题最开始我以为和HDU - 1542 那道题一样,只需要把cover次数改成2次即可,但是后面仔细一想,我们需要求的是覆盖次数大于等于2次的,这样的话,我们需要维护两个长度,HDU-1542 由于 ...

  9. 【评分】Beta 答辩总结

    [评分]Beta 答辩总结 总结 按时交 - 有分 晚交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 倒扣本次作业分数 由于前期不够重视,到beta评分才发现有5组的代码提交仅由一人&qu ...

  10. Bad Hair Day POJ - 3250 (单调栈入门题)

    Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-cons ...