在Angular的HTTP请求中正确设置授权头。例如,当使用基本身份验证时,可以将下面的代码添加到请求头中:
const headers = new HttpHeaders({
'Authorization': 'Basic ' + btoa(username + ':' + password)
});
this.http.get(url, {headers})
.subscribe(response => {
console.log(response);
}, error => {
console.error(error);
});
其中username
和password
是您的凭证,btoa()
函数将它们编码为Base64字符串,并将Authorization
头设置为Basic
加上编码后的凭证字符串。
如果您使用的是其他身份验证方法,可以根据需要设置适当的授权头。