2

I have a backend server (Spring Boot, proxied by nginx) that my Ionic application communicates with. With every single request, it sends the following headers: access-control-request-headers: *, access-control-allow-headers: *, access-control-allow-origin: *, access-control-allow-methods: GET, POST, PUT, HEAD, OPTIONS, DELETE, PATCH. No exceptions.

It also allows OPTIONS requests and returns HTTP 200 for every one of them.

Now, I have an ionic app that uses @angular/common/http. In browser, everything works fine, I can make GET and POST requests gracefully.

But in emulator (when I run with ionic cordova emulate ios, I can only make GET requests, POSTs fail with the following:

{"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}

As you can see, it says status: 0. This refers to a CORS issue, but I don't know how to further debug this.

My requests all succeed with Postman, and even with curl. It's only in emulator they fail.

Hasan Can Saral
  • 2,950
  • 5
  • 43
  • 78
  • Possible duplicate of [Ionic3(Angular4) CORS on an emulator](https://stackoverflow.com/questions/44463186/ionic3angular4-cors-on-an-emulator) – Hikmat G. Jul 19 '18 at 12:01
  • @HikmatGurbanli It is not. Did you even dare to read and compare the questions? I cannot use a proxy, as I am preparing to deploy my application. And every step in the referred question is already taken to no luck. – Hasan Can Saral Jul 19 '18 at 12:08
  • This is an oldie but a goodie: https://blog.ionicframework.com/handling-cors-issues-in-ionic/. This states there is no origin when running on device, thus CORS shouldn't come into play. Can you confirm there is no OPTIONS request when running in the emulator? Have you tried on a physical device? – BRass Jul 19 '18 at 12:09
  • @BRass Thanks, I am well aware of that page. As I stated, I cannot use a proxy, as stated on the blog. I am going to deploy. I can make `GET` requests, the problem is only with `POSTS`. The link you provided refers to the problem in general, where I have already taken all the steps mentioned. Though, only `GET`s are working. Thanks again. – Hasan Can Saral Jul 19 '18 at 12:12
  • Except length of the text seems same, no server issue works on browser, but not emulator. And you haven't mentioned if you have tried that – Hikmat G. Jul 19 '18 at 12:12
  • The status of 0 may be what you're getting on the ionic/angular level. Have you confirmed things on the HTTP request level (watch the actual HTTP traffic)? I've seen libraries hook into Angular that step on the actual HTTP status, leaving Ionic thinking it's a status 0. From what you have above I would be open to thinking this may be something other than CORS. – BRass Jul 19 '18 at 12:18
  • @HikmatGurbanli I am experiencing the same problem in the device as well. – Hasan Can Saral Jul 19 '18 at 12:56
  • @BRass `status: 0` means that the request is not made. I might agree that this might not be related to `CORS`. – Hasan Can Saral Jul 19 '18 at 12:58
  • `status: 0` in the Angular/Ionic layer does not necessarily mean the request is not made. Here's an example where an angular2-jwt module was stripping the actual HTTP response (HTTP 4xx's and 5xx's), and Angular got a `status: 0`. https://github.com/auth0/angular2-jwt/issues/408 – BRass Jul 19 '18 at 13:03

1 Answers1

1

CORS again, I am hoping that this might help someone:

Apparently wildcard(*) value for Acccess-Control-Allow-Headers header is only accepted in May 2016 (reference1, reference2), so some browsers might still not support it. So, changing from:

Access-Control-Allow-Headers: *

to

Access-Control-Allow-Headers: Content-Type, X-Auth-Token

(basically, the exact value of headers my application uses), I was able to resolve the issue. Apparently browser I am using on my desktop supports the wildcard header value, but the browser of the emulator and device doesn't. Definitely not a device vs. emulator issue, but a browser vs. browser issue.

Hasan Can Saral
  • 2,950
  • 5
  • 43
  • 78