未捕获(承诺):响应状态:401 未授权 URL
Uncaught (in promise): Response with status: 401 Unauthorized for URL
我正在尝试为我的路由器实施 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));
}
}
我收到此错误,未捕获(承诺):响应状态:401 未授权 URL。
当我收到该错误时,我的页面变成空白。
您需要使用 catch,并且 catch 必须 return 一个可观察对象。
validate() {
return this.http.get(this._apiEndPoint.validate)
.map(
(response: Response) => {
const result = response.json();
// check authentication status from response
return result.authenticated;
})
.catch(error => Observable.throw(error)); // If you want to throw nothing, then return Observable.empty()
}
我通过返回 false 的 observable 来修复
return this._auth.validate()
.map((isValidated) => {
if (isValidated && localStorage.getItem('authToken') && this.checkCookie()) {
return true;
}
}).catch(() => {
this.router.navigate(['/login']);
return Observable.of(false);
});
我的 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));
}
}
我收到此错误,未捕获(承诺):响应状态:401 未授权 URL。
当我收到该错误时,我的页面变成空白。
您需要使用 catch,并且 catch 必须 return 一个可观察对象。
validate() {
return this.http.get(this._apiEndPoint.validate)
.map(
(response: Response) => {
const result = response.json();
// check authentication status from response
return result.authenticated;
})
.catch(error => Observable.throw(error)); // If you want to throw nothing, then return Observable.empty()
}
我通过返回 false 的 observable 来修复
return this._auth.validate()
.map((isValidated) => {
if (isValidated && localStorage.getItem('authToken') && this.checkCookie()) {
return true;
}
}).catch(() => {
this.router.navigate(['/login']);
return Observable.of(false);
});