0

I'm trying to read the content from a file in a request GET using Angular Cli 11.0.6, I use this code to read the file:

 getFileText(url): Observable<any> {
  return this.http.get(url,{ responseType: 'text'})
  .pipe(
    retry(1),
    catchError(this.handleError)
  )
}

Then I run "getFileText()" method and put the content in my view:

  this.myServices.getFileText(this.url)
    .subscribe((data: any) => {
      this.txtcontent = data;
    });

This works good when I see it from my desktop browser. But when I tested from my phone mobile browser, using the same application, it doesn't work, it shows me the error:

Error code : 0 Message: Http failure response for http://urlfile.example : 0 Uknown error

I tried changing the responseType to JSON or Blob, but it shows me the same error, Does anyone know why this happens? Thanks.

Antonio
  • 115
  • 6
  • it requireds debugging. as it says the error is unknown. you could try to connect to your device with debug tools and try to see what is hapenning – Andrei Apr 04 '21 at 10:31
  • Hi, I tested with debug tools and I got this message error " from origin 'http://192.168.0.13:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.", How do I add to http options in my header? – Antonio Apr 05 '21 at 04:23
  • try to look the solution by "{your server technology} setup CORS" – Andrei Apr 05 '21 at 07:42

1 Answers1

0

If your server and angular app both are running in localhost, it can be a CORS origin-related issue.

You can refer to this link: I get "Http failure response for (unknown url): 0 Unknown Error" instead of actual error message in Angular

Mukul Kadel
  • 121
  • 1
  • 4
  • My angular app is localhost, the service that I'm trying to call is from a different server – Antonio Apr 05 '21 at 03:35
  • from origin '192.168.0.13:4200' has been blocked by CORS policy.... For this issue, fix will be applied at the backend server side. Approach will depend on the server used (which server?) but basically in the response headers you need to return "Access-Control-Allow-Origin":"*". "*" will allow API calls from all the IP addresses. – Mukul Kadel Apr 05 '21 at 05:21