博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue-axios当只调用vue.js又需要axios请求多时
阅读量:7042 次
发布时间:2019-06-28

本文共 3163 字,大约阅读时间需要 10 分钟。

可以将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)            })

 

转载于:https://www.cnblogs.com/weilizou/p/10843621.html

你可能感兴趣的文章
代码阅读总结之Fitch and Mather 7.0(自定义字符串缓存页)
查看>>
Ajax发送Post请求
查看>>
android文件导出错误—— failed to pull a selection
查看>>
PHP中的精确计算
查看>>
[CareerCup] 13.9 Aligned Malloc and Free Function 写一对申请和释放内存函数
查看>>
Cookies揭秘 [Asp.Net, Javascript] <转>
查看>>
数据库常见面试题总结
查看>>
jQuery源码分析系列(36) : Ajax - 类型转化器
查看>>
自定义分页标签详解
查看>>
Angularjs 事件指令
查看>>
杭城大医院特色科室全攻略请收藏
查看>>
32.2. munin-node and plugins
查看>>
c++类型形参的实参的受限转换
查看>>
VB.NET版机房收费系统---七仙女之系统登录
查看>>
强加密RNGCryptoServiceProvider
查看>>
Asp.net控件开发学习笔记(四)---Asp.net服务端状态管理
查看>>
数字油画指数又上升了
查看>>
领域驱动设计(DDD)的实践经验分享之持久化透明
查看>>
[Erlang 0007] Erlang ETS Table 二三事
查看>>
12.14. Switch Config Example
查看>>