【发布时间】:2019-01-23 18:25:37
【问题描述】:
我在使用 Angular 5 中的 httpinterceptor 时遇到了奇怪的问题。我无法在 Chrome 中获得错误响应和错误状态代码,但能够在下面的 IE 中获得我的 HttpInterceptor 代码。
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpResponse }
from '@angular/common/http';
import { finalize, tap } from 'rxjs/operators';
@Injectable()
export class LoggingInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
const startTime = Date.now();
let status: string;
return next.handle(req).pipe(
tap(
event => {
status = '';
if (event instanceof HttpResponse) {
status = 'succeeded';
}
},
error => {
if(error instanceof HttpErrorResponse) {
console.log(error.error.message)
// Above message is printing in IE but no in chorme.
}
}
),
finalize(() => {
})
);
}
}
在上面的错误中,我可以在 IE 中看到但在 Chrome 中看不到的消息和状态代码。请帮助我如何解决这个问题。
编辑: 我正在使用来自不同来源的数据,并且在 Web 服务中启用了 cors
【问题讨论】:
-
你试过了吗:
if(error instanceof HttpErrorResponse || error instanceof ErrorEvent)?或者只是不进行类型检查,看看 chrome 是否完全达到错误? -
您测试的实际 HTTP 错误是什么? Chrome 中是否抛出了 HTTP 错误?
-
我正在尝试使用服务引发的一些自定义消息来处理 500 内部服务器错误
-
这是 COR 请求还是同源请求?
-
这是一个 CORS 请求
标签: javascript angular angular5