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);
            })
         })
        }
    }
  • 相关阅读:
    SQL分页语句
    读、写 节点 XML方法总结
    新手入门:XHTML DHTML SHTML的区别
    ROS映射非21端口的FTP服务器设置
    图解:asp.net三种重定向方法
    在ASP.NET中显示进度条
    C#读取文本文件
    Net3.5都快来了,.Net2.0你们都知道多少呢?
    div+css布局漫谈
    自定义的向客户端输出Javascript脚本alert函数
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/8779037.html
Copyright © 2011-2022 走看看