我试图从Angular 4中的标头响应中获取Cookie。我编写了AuthenticationService,我在其中调用基于Cookie的Pentaho身份验证API。现在我想在点击登录后从标题中获取Set-Cookies button.on浏览器控制台我能够看到响应标头,但无法得到任何人建议我知道如何解决这个问题。
登录组件:
import { Component, OnInit } from '@angular/core';
import { Headers } from '@angular/http';
import { AuthenticationService } from '../service/authentication.service';
import { LoginInformationModel } from './logininformationdatamodel';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [AuthenticationService]
})
export class LoginComponent implements OnInit {
model: any = {};
loading = false;
error = '';
constructor( private authenticationService: AuthenticationService) { }
ngOnInit() {
}
login() {
this.loading = true;
this.authenticationService.login(this.model.j_username, this.model.j_password)
.subscribe(
res => { console.log("Success:");},
error => {
console.log("Error:");
this.loading = false;
}
);
}
}
的AuthenticationService
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import "rxjs/Rx"
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable()
export class AuthenticationService {
constructor(private http: Http) {
}
login(j_username: string, j_password: string): Observable<any> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('j_username', j_username);
urlSearchParams.append('j_password', j_password);
let body = urlSearchParams.toString()
let options = new RequestOptions({ headers: headers });
//return this.http.post('http://localhost:8083/pentaho/j_spring_security_check', body, {headers:headers})
//.map(res => res.json())withCredentials: true
return this.http.post('http://localhost:8083/pentaho/j_spring_security_check', body, options)
//.map(response => response.headers);
.map((res) => { res.json(); console.log('ma-ta'); })
.catch((error:any) => Observable.throw(error.message));
}
}