Wie fügen Sie Header zu Ihrer HTTP-Anfrage in Angular2 RC6 hinzu? Ich habe folgenden Code:http-Post - wie Autorisierungsheader senden?
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
}
Das Problem ist, ist, dass Winkel nicht Authorization-Header hinzufügen. Statt dessen in Aufforderung kann ich folgende zusätzliche Header sehen:
Access-Control-Request-Headers:authorization
Access-Control-Request-Method:POST
und sdch hinzugefügt in Accept-Encoding:
Accept-Encoding:gzip, deflate, sdch
Unfornately gibt es keine Authorization-Header. Wie sollte ich es richtig hinzufügen?
Ganze Anfrage von meinem Code gesendet sieht wie folgt:
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
Haben Sie versucht, stattdessen eigene Anfrageoptionen anzugeben? http://StackOverflow.com/a/35047978/4102561 – Supamiu
@Supamiu Unfornessly diese Lösung funktioniert nicht. Ich habe versucht: this.http._defaultOptions.headers.set ('Authorization', 'Basic YW5ndWxhci13YXJlaG91c2Utc2VydmljZXM6MTIzNDU2'); Und haben die gleichen Ergebnisse –
Die Anfrage, die Sie in Ihrer Frage zeigen, ist die 'OPTIONS' Preflight-Anfrage, können Sie bitte die POST-Anfrage hinzufügen (sollte direkt nach diesem in Ihrem Netzwerk Tab) – Supamiu