如何在Ionic 4修复android版本9的CORS问题

时间:2019-12-29 14:47:42

标签: android ionic-framework cors

我的Ioinc 4在chrome和firefox浏览器上运行,但是cors在android 9上无法正常工作。我的Http标头在下面。

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

常量令牌= localStorage.getItem('access_token');         this.loaderService.show();

    if (token) {
      request = request.clone({ setHeaders: { 
        'Authorization': token, 
        'Accept': 'application/json',
        'Access-Control-Allow-Origin':'*',
        'Access-Control-Allow-Methods':'*',
        'Access-Control-Allow-Headers': '*',
      }});
    } else {
      request = request.clone({ setHeaders: { 
        'Accept': 'application/json',
        'Access-Control-Allow-Origin':'*',
        'Access-Control-Allow-Methods':'*',
        'Access-Control-Allow-Headers': '*',
      }});
    }
    return next.handle(request).pipe(
      map((event: HttpEvent<any>) => {
        return event;
      }),
      catchError((error: HttpErrorResponse) => {
        return throwError(error);
      }),
      finalize(() => { 
        this.loaderService.hide();
       }));
  }

1 个答案:

答案 0 :(得分:0)

您可以将本机http插件用于http请求,此请求会自动处理,这是一个链接:https://ionicframework.com/docs/native/http