zoukankan      html  css  js  c++  java
  • react use axios拦截器

    import axios from 'axios';
    improt Promise from 'es6-promise';
    
    Promise.polyfill();
    
    const axiosService = axios.create();
    
    axiosService.defaults.timeout = 5000;
    axiosService.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    
    axiosService.interceptors.request.use(
        (config) => {
            if (config.data && config.data.$skipAuthHandler) {
                config.$skipAuthHandler = true;
                delete config.data.$skipAuthHandler;
            }
            if (config.params && config.params.$skipAuthHandler) {
                config.$skipAuthHandler = true;
                delete config.params.$skipAuthHandler;
            }
            config.headers.Authorization = getAuthorization();
            return config;
        },
        (error) => {
            return Promise.reject(error)
        }
    );
    
    axiosService.interceptors.response.use(
        (response) => {
            return response;
        },
        (error) => {
            const err = error.response;
            if (err.status === 401 && !! config.data && !config.data.$skipAuthHandler) {
                user.clear();
                window.location = '/unauthorization';
            }
            return Promise.reject(error);
        }
    );
    
    export default axiosService;

     update:

    import axios from 'axios';
    import {toastr} from "react-redux-toastr";
    
    //import LoginUser from "service/login-service/LoginUser";
    
    Promise.polyfill();
    
    const axiosService = axios.create();
    //const _loginUser = new LoginUser();
    
    axiosService.defaults.timeout = 5000;
    axiosService.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    
    axiosService.interceptors.request.use(
        (config) => {
            if (config.data && config.data.$skipAuthHandler) {
                config.$skipAuthHandler = true;
                delete config.data.$skipAuthHandler;
            }
            if (config.params && config.params.$skipAuthHandler) {
                config.$skipAuthHandler = true;
                delete config.params.$skipAuthHandler;
            }
            //config.headers.Authorization = _loginUser.getAuthorization();
            return config;
        },
        (error) => {
            return Promise.reject(error)
        }
    );
    
    axiosService.interceptors.response.use(
        (response) => {
            return response;
        },
        (error) => {
            const err = error.response;
            if (err.status === 401 && !! err.config && !err.config.$skipAuthHandler) {
                //_loginUser.clear();
                window.location = '/unauthorization';
            }
            toastr.error(err.data.message);
            return Promise.reject(error);
        }
    );
    
    export default axiosService;

    通用请求服务:

    import axiosService from 'axiosService';
    improt Promise from 'es6-promise';
    
    Promise.polyfill();
    
    export default class RequestService {
        axiosRequest(param) {
         return new Promise((resolve, reject) => {
            axiosService.request({
                url: param.url || '',
                method: param.method || 'GET',
                responseType: param.responseType || 'json',
                data: param.data || null,
                params: param.params || '',
            }).then(res => {
                typeOf resolve === 'function' && resolve(res);
            }).catch(error => {
                typeOf reject === 'function' && reject(error);
            })
         })
        }
    }
  • 相关阅读:
    android 开机启动
    android 获取lanucher 列表
    原创高端影楼人像专业磨皮法教程详解 附PSD源码
    [转]在SQLPLUS启动和停止Oracle数据库
    挑印刷时间最新的地图!
    Eclipse3.2下进行ArcGIS Server 9.2 Java WebADF开发手记 Eclipse使用技巧
    [藏]常用的匹配正则表达式和实例
    [藏]C# 中的常用正则表达式总结
    [转]使用uDig制作geoserver中需要的style
    [转]geoserver与OpenLayers配置入门
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/8779037.html
Copyright © 2011-2022 走看看