Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

在Postman中将token放在header中,在Angular中如何写?

已解决,答案在下面.

一.Postman中,访问成功:
image

二.Angular中 header设置错误:

描述: 第一次请求返回 token 后,第二次请求应该将 token 加在什么位置 ?

 getInfo(): Observable<any> {

const httpThis = this;
const headers = new HttpHeaders().set('Content-Type', 'application/json')
.set('Content-Type', 'application/x-www-form-urlencoded');

const body = '<tsRequest>' +
'<credentials name="admin" password="abc1#@365" >' +
'<site contentUrl=""/>' +
'</credentials>' +
'</tsRequest>';

const hdr = { headers };
return httpThis.http.post(this.uri_tableau + 'api/3.8/auth/signin', body, hdr)
.pipe(map(
(res: {credentials: Token}) => {
return res.credentials.token;
res.credentials.token);    // 拿到 token
}),

mergeMap((tokenRes: string) => {
headers.append('X-Tableau-Auth', tokenRes) //此处如何设置?
console.log(tokenRes)
return httpThis.http.get(this.uri_tableau + 'api/3.9/sites', hdr)
.pipe(map(
(sitesRes: {sites: SitesId}) => {
console.log(sitesRes.sites)
}

))

})

);

}   

报错如下(跨域问题已经解决):

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

使用append之后会生成一个新的header对象,如果没重新赋值,那么使用的还是之前那个


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...