【发布时间】:2017-04-23 15:46:04
【问题描述】:
我正在尝试将标题添加到我的http 发布请求中,如下所示
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
@Injectable()
export class UserService extends ServiceBase {
apiUrl: string;
private contentHeaders = new Headers();
constructor(private http: Http) {
super();
this.apiUrl = appConfig.apiBaseUrl + '/users';
}
login(user: User) {
this.contentHeaders.append('Accept', 'application/json');
this.contentHeaders.append('Content-Type', 'application/json');
return this.http.post(
this.apiUrl+'/sign_in',
JSON.stringify({user: user}),
{headers: this.contentHeaders}
);
}
}
Chrome DevTools 中显示的标头:
OPTIONS /api/v1/users/sign_in HTTP/1.1
Host: offers2win.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Access-Control-Request-Headers: access-control-allow-credentials, content-type
Accept: */*
Referer: http://localhost:3000/?
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
但在 Chrome 开发工具的网络面板中看不到这些标头。我在这里错过了什么。
【问题讨论】: