[英]Http interceptor Error Response in complete in chrome
I am facing weird issue while working with httpinterceptor in angular 5. I am not able to get the error response and error status code in Chrome and but able to get in IE below is my HttpInterceptor code. 我在使用角度为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(() => {
})
);
}
}
In the above Error block the message and status code I am able to see in IE but not in Chrome. 在上面的错误块中,我能够在IE中看到消息和状态代码,但在Chrome中却看不到。 Kindly help me how to resolve this.
请帮我解决这个问题。
Edit: I am consuming data from different origin and cors is enabled in web services 编辑:我正在使用来自不同来源的数据,并且在Web服务中启用了cors
I found the issue, It is due to not setting response headers on errors at server side. 我发现了这个问题,原因是没有在服务器端设置错误响应头。 Server is throwing exception directly for errors without setting response headers.
服务器直接针对错误抛出异常而不设置响应头。 I have set response headers at server side and now I am able to see the error message in chrome.
我在服务器端设置了响应标头,现在我能够在chrome中看到错误消息。
Still I am confused how come IE able to respond on this errors. 我仍然很困惑IE能够如何回应这个错误。
You are missing the HttpErrorResponse
import, a more browser agnostic approach would be to look at the error status, rather than the error message. 您缺少
HttpErrorResponse
导入,更多浏览器不可知的方法是查看错误状态,而不是错误消息。
if (err instanceof HttpErrorResponse) {
console.log(err.status);
}
I believe IE uses Friendly error messages where other browsers don't https://blogs.msdn.microsoft.com/ieinternals/2010/08/18/friendly-http-error-pages/ 我相信IE使用友好的错误消息,其他浏览器不会https://blogs.msdn.microsoft.com/ieinternals/2010/08/18/friendly-http-error-pages/
Here's a fairly up to date Medium article on Interceptors and usage which may clarify some things https://medium.com/@ryanchenkie_40935/angular-authentication-using-the-http-client-and-http-interceptors-2f9d1540eb8 这是一篇关于拦截器和使用的最新媒体文章,可能会澄清一些事情https://medium.com/@ryanchenkie_40935/angular-authentication-using-the-http-client-and-http-interceptors-2f9d1540eb8
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.