html - Angular 5 - HTTP拦截器拦截401作为状态码0
问题描述
我目前无法像大多数应用程序一样拦截 401 状态并重定向到登录页面,我的拦截几乎是样板文件
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
withCredentials: true
});
return next.handle(request)
.pipe(
retry(0),
takeUntil(this.httpCancelService.onCancelPendingRequests()), ==> cancels out pending request on route change
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
this.authenticationService.logout() ===> logout incase of 401
} else if (request.url.indexOf("assets") == -1) {
this.alertService.error('An unexpected error occurred, please try again later');
}
return throwError(errorMessage)
})
)
}
浏览器在“网络”选项卡上将请求捕获为 401,但在这个 Angular 拦截器上,我们得到“error.status”为“0”和“error.message”为“未知错误”
解决方案
状态码 0 表示可能是 CORS 错误。这意味着来自后端服务器的响应缺少Access-Control-Allow-Origin
标头。正确配置后端后,您将能够访问实际的错误代码。
推荐阅读
- xcode - ios中构建失败反应本机
- gatling - 如何按顺序运行不同协议的多个场景?
- javascript - Css 过渡不适用于 React 中的条件渲染
- python - 新 ckeditor 的插件未显示
- server - 从 V-Windows 服务器控制 Quasar?
- python - python和openpyxl:如何替换excel表上新出现的日期?
- amazon-web-services - 无法在 Centos 7 上使用 .NET Core SDK 读取 AWS 凭证
- c# - 如何在 Xamarin 表单中将 HTML 内容转换为 PDF
- xpath - XPATH 在某个单词后开始抓取
- javascript - Chromium 不接受 CSP script-src SHA-256 哈希