CORS issue on Android though http://localhost is added on Backend

Running mobile app using npx cap run andriod has CORS error though http://localhost is allowed on Backend.
If I run website using dist folder using serve -l 80 -s dist (website can be accessed http://localhost from chrome browser) and doesn’t have CORS error.

What is the actual error you are getting on the Android device? Also, are you running in the Android emulator or on a physical device?

Thank you @twestrick
I can’t inspect network request but the error happens as soon as I click login button. It happens on website as well (CORS issue) if I serve dist folder with different port.
I tried accessing deployed website in emulator browser and confirmed that network is available on emulator.

You can inspect the Android Emulator with Chrome Dev Tools by going here in Chrome - chrome://inspect/#devices.

It might not be a CORS issue. It could be that the app cannot access your API/backend due to a firewall port being blocked or some other issue. I’ve also seen that Chrome gives a CORS error when it is actually something else.

Also assuming you haven’t changed the server settings in the Capacitor Config overriding the hostname or androidScheme.

1 Like

Thank you very much @twestrick. I inspected using Chrome Dev Tools and it was related to ERR_CLEARTEXT_NOT_PERMITTED. I resolved it by adding following in capacitor.json

"server": {
    "cleartext": true

Cool. Hopefully you’ll be using HTTPS in production :slight_smile:

You can also set specific domains that are allowed under clear text if you don’t want it wide open.

Here is a reference - Unable to carryout HTTP post on Android - #2 by twestrick

EDIT: Also, if you use live reload with ionic cap run it auto adds clear text to your config temporarily as it runs as dev and not prod.