Angular 2 HTTP标头未添加到请求 [英] Angular 2 HTTP Headers not being added to request

查看:60
本文介绍了Angular 2 HTTP标头未添加到请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下http请求,在其中将标头添加到请求中,但是请求失败,并且当我在浏览器的网络"选项卡中查看时,请求标头不包含授权"标头.我在这里想念什么?

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

推荐答案

Headers 应该是 headers

this.http.get(address, { headers: headers })

这篇关于Angular 2 HTTP标头未添加到请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆