如何在Angular2 RC6中为你的http请求添加头信息? 我有以下代码:
login(login: String, password: String): Observable<boolean> {
console.log(login);
console.log(password);
this.cookieService.removeAll();
let headers = new Headers();
headers.append("Authorization","Basic YW5ndWxhci13YXJlaG91c2Utc2VydmljZXM6MTIzNDU2");
this.http.post(AUTHENTICATION_ENDPOINT + "?grant_type=password&scope=trust&username=" + login + "&password=" + password, null, {headers: headers}).subscribe(response => {
console.log(response);
});
//some return
}
问题是,Angular没有添加授权头。相反,在请求中我可以看到以下附加标头:Access-Control-Request-Headers:authorization
Access-Control-Request-Method:POST
并在 Accept-Encoding 中添加了 sdch:
Accept-Encoding:gzip, deflate, sdch
很遗憾,没有授权标头。我应该如何正确添加它?
我的代码发送的整个请求如下所示:OPTIONS /oauth/token?grant_type=password&scope=trust&username=asdf&password=asdf HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://localhost:3002
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
Referer: http://localhost:3002/login
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,pl;q=0.6
OPTIONS
预检请求,请问你能否添加 POST 请求?(应该紧随此请求在你的网络选项卡中) - Supamiu