2016-03-07 71 views
0

編輯: 原來這是在Apache配置服務器端解決的。但值得注意的是,授權頭實際上包含在預檢請求標頭中,而不是主要請求中,這就是爲什麼它看起來不在標頭中。

我有這個問題,這仍然是懸而未決描述了同樣的問題:Angularjs set a authorization header

我需要在我的請求頭授權令牌。

app.config(function($httpProvider) { 
    $httpProvider.defaults.headers.common['Authorization'] = '1234567'; 
}); 

而且這樣的:我有這樣的使用方法 - 一些我試着設置$ HTTP默認值如下試圖

app.run(function($http) { 
    $http.defaults.headers.common['Authorization'] = '1234567'; 
}); 

此外,我試圖構建我的請求,這樣在一個方法我服務:

var request = { 
    method: 'GET', 
    url: 'my/api/endpoint', 
    headers: { 
     'Authorization': '1234567' 
    } 
}; 

$http(request).then(function(response) { 
    console.log(response); 
}); 

在所有這些情況下,我最終在我的請求頭看到的是這樣的:

Access-Control-Request-Method: GET 
Origin: http://localhost:8000 
Access-Control-Request-Headers: accept, authorization 
Accept: */* 
Referer: http://localhost:8000/ 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-US,en;q=0.8 

我希望看到有另一種鍵值對,如:

Authorization: '1234567' 

但是,我不知道。

我正在使用Angular版本1.4.9,不知這個問題是否特定於此版本?

回答

1

我在Interceptor中執行它,它可以攔截所有請求並將Auth添加到header.Maybe你可以嘗試它。  [like this

+0

它最終成爲服務器端的問題,但仍然是upvoting,因爲這是一個很好的建議。 :) – kfhohn