vue-axios当只调用vue.js又需要axios请求多时

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

猜你喜欢

转载自www.cnblogs.com/weilizou/p/10843621.html
今日推荐