我正尝试访问API并碰壁。我已经阅读了Angular文档,但仍然缺少一些内容。请指教。谢谢!
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map, catchError, tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class RestService {
private url = 'https://api.com';
private auth = ('xxxx:xxxx');
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Content-Encoding': 'none',
'Authorization': 'Basic' + btoa(this.auth)
})
};
constructor(private http: HttpClient) {
}
index() {
return this.http.get<any[]>(this.url, this.httpOptions);
}
}
答案 0 :(得分:0)
在您的授权标头上,在单词Basic和您的auth密钥之间创建一个空格。像这样:
'Authorization': 'Basic ' + btoa(this.auth)