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没有进行封装复用,项目越来越大,引起的代码冗 ...
随机推荐
- PAT A1094 The Largest Generation (25 分)——树的bfs遍历
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- 【thinkPHP框架】Failed opening required 'header.php' include_path='.;c:\php5\pear 终级解决方案
ThinkPHP框架中的某一个php页面,想要去引入其他php页面,结果反复报错,无解. 各种百度,有说是文件权限不够的,也有说配置不对的,反正一个都没有解决这个问题,依然无解. 最终,找到了办法,似 ...
- [翻译] ASP.NET Core 2.1.0 发布
原文: ASP.NET Core 2.1.0 now available 今天,我们很高兴可以发布 ASP.NET Core 2.1.0!这是我们 .NET平台下开源的.跨平台的 Web 框架的最新版 ...
- 初步学习Xamarin的感受
一直仰慕Xamarin的大名,最近抽空去浅学了一下. 最后有一种这东西不咋地,又有一种这东西还不错的感觉 先说下为什么不咋地? 如果在公司项目使用Xamarin.forms这个东西.按照国内APP设计 ...
- 微软是如何让我再次爱上.Net Core和C#的
“为什么你还想用ASP.NET,难道你还活在90年代吗?”这正是我的一位老同事在几年前我们即将开始的项目中我提出考虑使用ASP.NET时所说的话.当时我很大程度上认同他的看法,微软已经开发了伟大的开发 ...
- 使用 Emmet 生成 HTML 的语法详解
生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 “!” 就可以生成一个 HTML5 的标准文档 ...
- c++入门之—运算符重载和友元函数
运算符重载的意义是:将常见的运算符重载出其他的含义:比如将*重载出指针的含义,将<<与cout联合使用重载出输出的含义,但需要认识到的问题是:运算符的重载:本质仍然是成员函数,即你可以认为 ...
- 出题人的女装(牛客练习赛38题B) (概率+分式运算)
链接:https://ac.nowcoder.com/acm/contest/358/B来源:牛客网 出题人的女装 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
- org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manu
这个是sql 语句 错误 仔细检查 SQL语句是否写错了 org.apache.ibatis.exceptions.PersistenceException: ### Error queryi ...
- 自己实现数据结构系列五---BinarySearchTree
一.二分搜索树: 1.代码: public class BST<E extends Comparable<E>> { private class Node{ public E ...