error httpclient doesn't work in android browser work's in chrome

235 Views Asked by At

error screenshot

post, get, etc. methods http works in the desktop browser but not in android browser, in chrome for mobile it works fine

import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { retry, catchError, map } from 'rxjs/operators';
import { Observable, throwError, concat, of, BehaviorSubject, Subject, EMPTY } from 'rxjs';

export class HttpService {
  private REST_API_SERVER: string = `${environment.baseUrl}`;

  constructor(private http: HttpClient) {}

  httpOptions = new HttpHeaders({
    'Content-Type': 'application/json'
  });

  private _modalOpen: BehaviorSubject<any> = new BehaviorSubject<any>(null);

  private modalOpenObservable = new Subject<any>();
  modalErrorOpen$ = this.modalOpenObservable.asObservable();

  handleError(error: HttpErrorResponse) {
    let errorMessage = 'Unknown error!';
    if (error.error instanceof ErrorEvent) {
      // Client-side errors
      errorMessage = `Error: ${error.error.message}`;
    } else {
      // Server-side errors
      errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;

      if ((error.status >= 400 && error.status <= 600) || error.status == 0) {
        errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
      }
    }

    console.warn(errorMessage);

    const result = concat(of(error.status), throwError(new Error('oops!')));
    result.subscribe(x => console.log(x), e => console.log(e));

    return result;
  }

  validateToken() {
    this.headersWhithToken = new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization: `${this.getToken()}`
    });
  }

    public postRequest<T>(url: string, data: any, headers: any): Observable<any> {
      if (headers === false) {
        return this.http.post<T>(this.REST_API_SERVER + url, JSON.stringify(data), { headers: this.httpOptions }).pipe(
          retry(3),
          catchError(() => {
            this.modalOpenObservable.next(true);
            return EMPTY;
          }),
          map(object => object)
        );
      } else {
        this.validateToken();
        return this.http
          .post<T>(this.REST_API_SERVER + url, JSON.stringify(data), { headers: this.headersWhithToken })
          .pipe(
            retry(3),
            catchError(() => {
              this.modalOpenObservable.next(true);
              return EMPTY;
            }),
            map(object => object)
          );
      }
    }
}

in the component the call to the http method is made, when the call is made in the desktop browser everything goes well, in the android browser the image error is shown...

in component:

sendValues(data) {
if (this._authService.token != '') {
  const url = `${SERVICES.RADAR.CODE}`;
  this.headers = true;
  this._http.postRequest<OffersInterface[]>(url, data, this.headers).subscribe(dataOffers => {
    this.idOffer = dataOffers.url;
    this.thankYouPage();
  });
} else {
  const url = `${SERVICES.RADAR.ANONYMOUS}`;
  this.headers = false;
  this._http.postRequest<OffersInterface[]>(url, data, this.headers).subscribe(dataOffers => {
    this.idOffer = dataOffers.url;
    this.thankYouPage();
  });
}

}

0

There are 0 best solutions below