在get request angular 2上设置标题

Adr*_*ola 16 get header xmlhttprequest cors angular

我尝试将Authorization标头设置为GET请求,以将用户身份验证为其他API.我正在使用Angular 2 RC1.(我是初学者).

getTest(){
    let authToken = localStorage.getItem('auth_token');
    let headers = new Headers({ 'Content-Type': 'application/json' });
    headers.append('Authorization', `Bearer ${authToken}`);

    let options = new RequestOptions({ headers: headers });
    return this._http
      .get(this._url,options)
      .map(res => console.log(res));
  }
Run Code Online (Sandbox Code Playgroud)

我在后端允许CORS.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Request-Headers: Content-Type, Authorization");
Run Code Online (Sandbox Code Playgroud)

我的控制台:

选项api/userProfile /

XMLHttpRequest无法加载/ userProfile /.预检的响应具有无效的HTTP状态代码406

我的请求标题

任何的想法 ?

Thi*_*ier 26

我认为您需要Accept标题而不是因为406状态代码...

let authToken = localStorage.getItem('auth_token');
let headers = new Headers({ 'Accept': 'application/json' });
headers.append('Authorization', `Bearer ${authToken}`);

let options = new RequestOptions({ headers: headers });
return this._http
  .get(this._url,options)
  .map(res => console.log(res));
Run Code Online (Sandbox Code Playgroud)

这允许您告诉服务器您在响应中期望的内容类型...

Content-Type头是相当指定你的请求中发送的内容的类型.在你的情况下,没有内容......