如何解决在Get方法Angular 9上获得401未经授权未提供身份验证凭据
我遇到授权问题。当我尝试从服务器获取数据时,我得到了
error: {detail: "Authentication credentials were not provided."}
headers: HttpHeaders {normalizedNames: Map(0),lazyUpdate: null,lazyInit: ƒ}
message: "Http failure response for http://someDomein.com/api/v1/users/: 401
Unauthorized"
name: "HttpErrorResponse"
ok: false
status: 401
statusText: "Unauthorized"
我的服务是
export class UserService {
public userList: User[] = [];
headers: Headers = new Headers();
constructor(private http: HttpClient) {}
fetchUsers(): Observable <User[]> {
const headers = new HttpHeaders({
'Content-Type': 'application/json',});
this.headers.append('Authorization','Bearer ' + localStorage.getItem('auth-token'));
const options = {
headers
};
return this.http.get<User[]>('http://someDomain.com/api/v1/users/',options)
.pipe( tap(userList => this.userList = userList));
}
}
我的中间人是
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private auth: AuthService) {}
intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
const token = this.auth.getToken();
req = req.clone({
setHeaders: {
'Content-Type': 'application/json',Authorization: `${token}`
},});
return next.handle(req);
}
}
我在此网站上检查了很多答案,但没有找到答案。 预先感谢您的帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。