zoukankan      html  css  js  c++  java
  • 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)
                })
  • 相关阅读:
    递归函数之阶乘和字符串反转-基于R和Python
    ERROR getting 'android:label' attribute: attribute is not a string value
    CefGlue 学习杂记
    WinDbg 解决Font.ToLogFont AccessViolationExcetion
    使用ActivityManager的forceStopPackage方法结束进程
    (转) lucene+paoding亲密接触
    (转)Lucene中文分词图解
    (转)实战 Lucene,第 1 部分: 初识 Lucene
    Python时间戳的使用
    Andriod中Style/Theme原理以及Activity界面文件选取过程浅析
  • 原文地址:https://www.cnblogs.com/weilizou/p/10843621.html
Copyright © 2011-2022 走看看