zoukankan      html  css  js  c++  java
  • angular 用拦截器统一处理http请求和响应 比如加token

    想使用angularjs里的htpp向后台发送请求,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}}

    index.html里引入以下js:

    angular.module('app.factorys',[])
        .factory('httpInterceptor',['$q','$injector','$localStorage',function ($q,$injector,$localStorage) {
            var httpInterceptor = {
                'responseError' : function(response) {
                    // ......
                    return $q.reject(response);
                },
                'response' : function(response) {
                    if (response.status == 21000) {
                        // console.log('do something...');
                    }
                    return response || $q.when(response);
                },
                'request' : function(config) {
                    config.headers = config.headers || {};
                    if ($localStorage.token) {
                        config.headers.token = $localStorage.token;
                        // config.headers['X-Access-Token'] = $localStorage.token;
                    };
    
                    return config || $q.when(config);
    
                    return config;
                },
                'requestError' : function(config){
                    // ......
                    return $q.reject(config);
                }
            };
            return httpInterceptor;
        }])
     

    在app里注入factory后,在config里面配置

    .config(['$httpProvider',function(){
        $httpProvider.interceptors.push(httpInterceptor);
    }])

    如果你的代码并未做拆分,可以直接在config里面写拦截器

     $httpProvider.interceptors.push(['$q','$injector','$localStorage',function ($q,$injector,$localStorage) {
                var httpInterceptor = {
                    'responseError' : function(response) {
                        // todo...
                        return $q.reject(response);
                    },
                    'response' : function(response) {
                        if (response.status == 21000) {
                            // console.log('do something...');
                        }
                        return response || $q.when(response);
                    },
                    'request' : function(config) {
                        config.headers = config.headers || {};
                        if ($localStorage.token) {
                            config.headers.ut = $localStorage.token; //把你登录接口返回给你的token存到$localStorage里面,在这里取就好了
                            // config.headers['X-Access-Token'] = $localStorage.token;
                        };
    
                        return config || $q.when(config);
                        // return config;
                    },
                    'requestError' : function(config){
                        // todo...
                        return $q.reject(config);
                    }
                };
                return httpInterceptor;
            }]);
  • 相关阅读:
    [开源项目]蓝点无限TWR算法-多基站多标签固件
    [开源项目] 蓝点无限 UWB Python版本上位机
    记一次RabbitMQ的脑裂(网络分区)问题
    使用Docker持久化部署SQL Server
    .NET---Exceptionless 轻量级的分布式日志管理平台
    python性能测试工具locust
    Javascript —— 线转树 or 树转线
    记录一个生僻知识点 —— JS字符模板替换
    车证识别工具|行驶证识别工具|行驶证识别OCR工具免费版V3.0.0.0
    C# CAD 凹凸点识别最大轮廓
  • 原文地址:https://www.cnblogs.com/cynthia-wuqian/p/6958659.html
Copyright © 2011-2022 走看看