首页 > 解决方案 > 有没有办法跳过Angular httpClient未处理的预检响应?

问题描述

我们正在使用 httpClient 和 withCredentials: true 开发一个 Angular 6 应用程序。当我们向同一服务器上的另一个端口(从端口 8010 到 8011)发出请求时,Firefox 会尝试发出预检请求。但似乎我们的角度 httpClient 正在尝试处理这个预检响应。我们正在使用下面的代码。我们看到,流程正在进入 catchError 块。为什么 angular-httpclient 处理预检响应?我们如何处理这个?

request(url: string, method: string, options?: any, xmlOptions?: any,
    isFullUrl:boolean = false): Observable<any> {
    const fullUrl = (isFullUrl) ? url : `${HttpService.BASE_URL}${url}`;
    return this.httpClient.request(method, fullUrl, options).pipe(
        map(resp => this.parseXML(resp, xmlOptions)),
        catchError(exception => this.processError(exception, xmlOptions))
    );
}

private processError(exception, xmlOptions) {
    this.notificationService.clear();
    this.notificationService.error(HttpService.UNEXPECTED_ERROR_OCCURRED);
    if(exception.error) {
        let errorObj = this.parseXML(exception.error, xmlOptions);
        return throwError(errorObj);
    }
    else {
        return throwError(exception);
    }
}

如果我们检查异常,错误消息是"Http failure response for (unknown url): 0 Unknown Error"

注意:这在 Chrome 浏览器中运行良好。

标签: angular6angular-httpclient

解决方案


推荐阅读