gpt4 book ai didi

angular - 未捕获( promise ): Response with status: 401 Unauthorized for URL

转载 作者:搜寻专家 更新时间:2023-10-30 21:23:48 25 4
gpt4 key购买 nike

enter image description here我正在尝试为我的路由器实现 authGuard。但是当我干扰错误代码时,应用程序实际上会中断。我不知道如何解决它。需要帮助。

我的 AuthGuard

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {

return this._auth.validate()
.map((isValidated) => {
if (isValidated && localStorage.getItem('authToken') && this.checkCookie()) {
return true;
}
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
});
}

我的授权服务

@Injectable()
export class AuthService {

loginStatus;

constructor(private http: Http, private _apiEndPoint: ApiEndPoint, private router: Router) { }

validate() {

return this.http.get(this._apiEndPoint.validate)
.map(
(response: Response) => {

const result = response.json();

// check authentication status from response
if (result.authenticated) {
return true;
} else {
return false;
}
})
.catch(error => Observable.throw(error));
}

}

我收到此错误,Uncaught( promise ):响应状态:401 Unauthorized for URL。

ScreenShot of my Error

当我收到该错误时,我的页面变成空白。

最佳答案

我通过返回 observable of false 来修复

return this._auth.validate()
.map((isValidated) => {
if (isValidated && localStorage.getItem('authToken') && this.checkCookie()) {
return true;
}
}).catch(() => {
this.router.navigate(['/login']);
return Observable.of(false);
});

关于angular - 未捕获( promise ): Response with status: 401 Unauthorized for URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43697084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com