有沒有方法在angular.module('myApp', []).config()
之外設置$httpProvider
標頭?在AngularJS中設置應用程序範圍廣的HTTP標頭
我在登錄用戶後從服務器獲取Auth-Token,我需要將它作爲HTTP標題添加到所有以下請求中。
有沒有方法在angular.module('myApp', []).config()
之外設置$httpProvider
標頭?在AngularJS中設置應用程序範圍廣的HTTP標頭
我在登錄用戶後從服務器獲取Auth-Token,我需要將它作爲HTTP標題添加到所有以下請求中。
$http.defaults.headers.common['Auth-Token'] = 'token';
看起來headers()
規範化了鍵名。
你可以使用默認的標頭的角度1.0.x的:
$http.defaults.headers.common['Authentication'] = 'authentication';
或角1.1.x版本+請求攔截器:
myapp.factory('httpRequestInterceptor', function() {
return {
request: function (config) {
// use this to destroying other existing headers
config.headers = {'Authentication':'authentication'}
// use this to prevent destroying other existing headers
// config.headers['Authorization'] = 'authentication';
return config;
}
};
});
myapp.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
});
由於工廠/服務是單身,這隻要您在實例化服務後不需要動態更改「身份驗證」值即可。
添加到@Guria和@Panga
config.headers['X-Access-Token'] = $window.sessionStorage.token;
一個以上響應可以用x存取令牌標頭,如JWT(jsonwebtoken)。 當用戶首次進行身份驗證時,我將JWT存儲在會話存儲中。
你能詳細說明你的意思嗎?規範化的關鍵名稱? – Webnet
使用headers()方法獲取標題時,「Auth-Token」鍵變小,變成「auth-token」。這是令人困惑的。 – lucassp
@lucassp可能是這個 - http://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive – KrIsHnA