這是我在角4的應用程序添加頁眉到一個http請求的方式:添加額外的頭到一個HTTP請求
constructor(private http: Http) { }
getUsers() {
const headers = new Headers(
{
'Content-Type': 'application/json',
'Authorization': 'somekey ' + localStorage.getItem('token')
});
return this.http.get(environment.apiUrl + 'users/all', {headers: headers})
}
我的API工作正常,當我通過郵差調用它。當我檢查結果
request.getHeader("Authorization")
它返回適當的值,這是我的令牌。
但是當我通過前端應用
request.getHeader("Authorization")
返回空調用API。還會創建一個名爲「access-control-request-headers」的標頭,其值爲:「authorization,content-type」。 我設置的令牌在哪裏?爲什麼這個請求沒有被授權得到結果,就像我使用Postman調用API一樣?
你用的console.log得到(localStorage.getItem( '令牌')) ;這樣對嗎? – Vega
對不起,我剛剛看到你的評論。是的,我在localStorage中擁有正確的價值。我解決了這個問題。看看下面的答案。 – Alireza
沒問題,很高興你解決了它。 – Vega