Ionic 4 android device CORS issue

Hi,

I am setting up my device to pull rest api and it works out well on the localhost:8100 on my browser where I dont have any issues on the cors, as I already set it up on the server to Access-Control-Allow-Origin to *

but somehow in the device directly it keeps on hitting the localhost being block and no Access-Control-Allow-Origin was found…

can someone help?

On iOS there is a problem with * for Access-Control-Allow-Origin. Maybe it’s the same on Android. So, you could try to specify the origin explicitly:

Android: http://localhost
iOS: ionic://localhost

You should also configure Access-Control-Allow-Headers in your backend to specifiy all the headers you send.

I have done this from my server: and the login POST works fine but some how I checked my code :slight_smile:

return this.httpClient.post(‘http://www.xxxxx.com/reguser?_format=json’,
{
name: form.value[‘mail’],
email: form.value[‘mail’],
password: form.value[‘pass’]
},
{headers: { ‘Content-Type’: ‘application/json’}}
).subscribe(
data => {
console.log(data);
},
error => {
console.log(error);
},
() => {
}
);

and the header response :slight_smile:

  1. Request Method:

OPTIONS

  1. Status Code:

200 OK

  1. Remote Address:

23.185.0.1:80

  1. Referrer Policy:

no-referrer-when-downgrade

which is strange, why my POST becomes OPTIONS?

With CORS enabled a pre-flight request (OPTIONS) is sent before the POST request. See https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request for an explanation.

yes but its loading 409conflict?

Which request? OPTIONS or POST?
Maybe there is something wrong with your request. But the problem should be located in the server.

OPTIONS part, login is okay, only register which is weird, but this is happening only on device not the debugging on browser part

Any hints in the logs of the server?

nothing at the server end I checked

@bobyui Did you ever find a resolution to this issue? All requests work fine for me over browser and emulation, BUT when making requests from actual device, fails with CORS.

Please make sure to use HTTPS endpoints.