Angular不会在每个请求上发送标头

时间:2017-05-29 04:15:18

标签: angularjs

我正在使用角1.6.4。我正在尝试在每个请求上发送一个Auth标头,但客户端似乎没有在请求中发送标头。不确定我做错了什么......

这是我的app.js

angular.module("myApp", ['ngRoute', 'ngMaterial', 'toolbar', 'authService'])
    .config(function ($httpProvider) {
        $httpProvider.interceptors.push('AuthInterceptor');
    });

和我的AuthInterceptor

angular.module('authService', [])
.factory('AuthInterceptor', function() {
    return function(config) {
        request: {
            config.headers.Authentication = 'thisIsATestToken';
            return config;
        }
    };
});

但是,在服务方面,我从未获得“身份验证”标题。

我看过我的例子,这正是所有人都这样做的。我只是在localhost上本地运行应用程序。

2 个答案:

答案 0 :(得分:1)

更改您的身份验证截取工厂,如下所示

angular.module('authService', [])
    .factory('AuthInterceptor', function() {
        return {
            request: function(config) {
                config.headers.Authentication = 'thisIsATestToken';
                return config;
            }
        };
    });

答案 1 :(得分:0)

您可以在主模块中使用此方法

angular.module('myApp', [])
    .run(['$http', function($http) {
      $http.defaults.headers.common.Authorization = 'thisIsATestToken';
    }])