我想在响应头(Set-Cookie)中获取会话令牌.如何才能访问Response头中的值?
var headers = new Headers();
headers.append('Content-Type', 'application/json');
console.log('url',this.loginUrl)
this.http.post(this.loginUrl,
JSON.stringify({ "username": value.username, "password": value.password }),
{ headers: headers })
.map((res: Response) =>
res.json())
.subscribe((res) => {
console.log("res", res);
this.loading.hide();
if (res.message_code == "SUCCESS") {
this.nav.setRoot(HomePage, {
username: value.username,
});
} else {
let alert = Alert.create({
title: "Sign In Error !",
subTitle: 'Please Check Username or Password.',
buttons: ['Ok']
});
this.nav.present(alert);
}
}, err => {
this.loading.hide();
console.log('error', err);
});
Run Code Online (Sandbox Code Playgroud)
这是我的标题回复
我正在使用一个登录端点,它返回一个不记名令牌作为响应标头,正如我在“网络”Chrome 检查窗口中看到的那样:
Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8100
Authorization:Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJuZWxpby5jdXJzb3NAZ21haWwuY29tIiwiZXhwIjoxNTEyNzA3OTQ3fQ.pOR4WrqkaFXdwbeod1tNlDniFZXTeMXzKz9uU68rLXEWDAVRgWIphvx5F_VCsXDwimD8Q04JrxelkNgZMzBgXA
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:188
(etc...)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用 HttpClient 实例从响应中打印“标题”时:
authenticate(credentials) {
let creds = JSON.stringify(credentials);
let contentHeader = new HttpHeaders({"Content-Type": "application/json"});
this.http.post(this.LOGIN_URL, creds, { headers: contentHeader, observe: 'response'})
.subscribe(
(resp) => {
console.log("resp-ok");
console.log(resp.headers);
},
(resp) => {
console.log("resp-error");
console.log(resp);
}
);
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个完全不同的结构:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
lazyInit : ƒ ()
lazyUpdate : null
normalizedNames : Map(0) {}
Run Code Online (Sandbox Code Playgroud)
我也尝试了 get(headerName) 方法并得到了空值。我错过了什么?如何从我的回复中获取“授权”标头?
我在Chrome开发工具中看到的响应标头与Angular 2在Chrome控制台中显示的响应标头不匹配.
执行后只显示Content-Type标题:
this.http.get(tempUrl).map(res=>{
console.log("csrf received");
console.log(res);
})
Run Code Online (Sandbox Code Playgroud)