2017-05-29 41 views
0

我正在使用angular 1.6.4。我試圖在每個請求上發送一個Auth頭文件,但客戶端似乎沒有在請求中發送頭文件。不知道我做錯了什麼......Angular不會在每個請求上發送頭文件

這是我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上本地運行應用程序。

+1

你確定發送'Authentication'頭,或者是' Authorization'? – 31piy

+1

看看你的工廠,你正在返回函數,而不是對象字面值,也使用'request:function(config){....' – Kanagu

回答

0

您可以使用此方法的主要模塊本身中

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

我也試過了。當我查看請求時,標題未被髮送。 – Tony

1

您的身份驗證截擊工廠更改爲類似下面

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