可以将axios方法封装一个函数

(function () {
ASK = {
get:function (url,data,succFun,errFun) {
axios.get(url,{
params:data,
headers:{
"token":""
}
})
.then(function (response) {
if(response.data.code=='200'){
if (succFun){
succFun(response.data);
}
}else if(response.data.code=='401'){
alter('请求超时,请重新登录')
window.location.href='login.html'
}else{
console.log(response.data)
}
})
.catch(function (error) {
if (errFun){
errFun(error);
}
});
},
post:function (url,data,succFun,errFun) {
axios.post(url,data,{
headers:{
"token":"",
'Content-Type':'application/x-www-form-urlencoded'
}
})
.then(function (response) {
if(response.data.code=='200'){
if (succFun){
succFun(response.data);
}
}else if(response.data.code=='401'){
alter('请求超时,请重新登录')
window.location.href='login.html'
}else{
console.log(response.data)
}
})
.catch(function (error) {
if (errFun){
errFun(error);
}
});
},
other:function (url,requestType,data,succFun,errFun) {
axios({
method: requestType,
url: url,
data: data,
headers:{
"token":""
}
}).then(function (response) {
console.log(response);
if (succFun){
succFun(response.data);
}
}).catch(function (error) {
console.log(error);
if (errFun){
errFun(error);
}
})
}
};
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
/**
* 请求之前操作:success
*/ return config;
}, function (error) {
/**
* 请求之前操作:failure
*/ return Promise.reject(error);
}); // 添加响应拦截器
axios.interceptors.response.use(function (response) {
/**
* 响应之前操作:success
*/ return response;
}, function (error) {
/**
* 响应之前操作:failure
*/ return Promise.reject(error);
});
})(axios);

调用时

ASK.get(CDK.host+'/cdk/company/findList',{
"pageNum":this.pageNum,
"pageSize":this.pageSize
},(res)=>{
console.log(res.obj)
},(err)=>{
console.log(err)
})

最新文章

  1. Fiddler基本原理与抓取Andriod与IOS的App数据包
  2. 【转】APNs消息推送完整讲解
  3. 【转】Hadoop web页面的授权设定
  4. HNOI2006-鬼谷子的钱袋
  5. Smarty插件简单开发
  6. Java实现二维码QRCode的编码和解码
  7. python手记(50)
  8. thinkphp3.2 namespace及use用法
  9. C语言的本质(28)——C语言与汇编之用汇编写一个Helloword
  10. SDK Hello world(直接使用SDK封装)
  11. 10.读google测试之道有感
  12. Spring Boot 学习笔记--整合Redis
  13. NumPy 超详细教程(1):NumPy 数组
  14. android:layout_weight属性详解
  15. 【核心】project(idea文件)、module(iml文件)到SSM集成、热部署、Tomcat启动、MAVEN依赖冲突
  16. Stm32串口通信(USART)
  17. Visual Studio AI环境记录(Windows10)
  18. 浅谈JS的变量提升
  19. 微信小程序调用接口返回数据或提交数据
  20. java中并发Queue种类与各自API特点以及使用场景!

热门文章

  1. 实现手机网页调起原生微信朋友圈分享的工具nativeShare.js
  2. CentOS8.0-1905安装配置ftp服务器
  3. 10种简单的Java性能优化(转)
  4. 国外最受欢迎的十大社交APP网站
  5. jmeter进行的接口测试和压力测试
  6. LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
  7. MySql5.7 配置文件 my.cnf 设置
  8. C-链表实现,保存文件,评估-单项选择题系统课程设计---ShinePans
  9. init()方法必须使用super.init(config)的原因--Servlet
  10. [软考]之软件过程模型I 标签: 总结软考 2015-10-24 11:58 863人阅读 评论(35) 收藏