Angular 4如何读取标头中的令牌? [英] Angular 4 How to read the token in the header?
本文介绍了Angular 4如何读取标头中的令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新手.我无法读取REST服务响应标头中的令牌(jwt).如何阅读标题?这是我的代码:
I am new to angular. I can not read the token (jwt) in the REST service response header. How can I read headers? this is my code:
loginUser(loginUser:Login,callback: (data) => void){
let body = JSON.stringify(loginUser)
const headerJson = {
'Content-Type': 'application/json'
}
let options = { headers: new HttpHeaders( headerJson ) };
return this.http.post(this.config.datiBean.urlLoginToken, body,options).subscribe(
res =>{
console.log("body :"+JSON.stringify(res)); //ok
callback(res);
}
/// at this point how can I read headers?
)};
}
我想阅读以下信息:
感谢您的帮助
推荐答案
我的方法很可能不正确.我这样修改了我的方法:
Most likely my method was not correct. I modified my method this way:
loginUser(loginUser:Login,callback: (data) => void){
let creds = JSON.stringify(loginUser);
let contentHeader = new HttpHeaders({ "Content-Type":"application/json" });
this.http.post(this.config.datiBean.urlLoginToken, creds, { headers: contentHeader, observe: 'response' })
.subscribe(
(resp) => {
console.log("TOKEN: "+resp.headers.get('X-Auth'))
console.log("body: "+JSON.stringify(resp.body))
callback(resp)
},
(resp) => {
console.log("resp-error");
console.log(resp);
}
);
};
现在它可以正常工作了
这篇关于Angular 4如何读取标头中的令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文