问题
I have the following http request in which I add the header to the request, however the request fails and when I look in the Network tab of the browser the request headers don't include the Authorization header. What am I missing here?:
public get(address: string, callback: any): Observable<any> {
let headers: Headers = new Headers();
headers.append('Authorization', 'Bearer <token>');
return this.http.get(address, { 'Headers': headers })
.map(callback)
.catch((error: any) => this.handleError(error));
}
Request Headers from the Network tab:
GET /v1/projects HTTP/1.1
Host: <host>
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
Accept: */*
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
回答1:
Headers
should be headers
this.http.get(address, { headers: headers })
来源:https://stackoverflow.com/questions/40159973/angular-2-http-headers-not-being-added-to-request