[Solved] Access-Control-Allow-Origin not working with ionic 5 app [closed]


I found the problem and the fix. As expected it was in the Ionic App’s code. But it was not quite what I expected. Hopefully, this may be useful to someone in the same predicament. The problem was this line:

base_path="http://mywebsite.com/App";

This was supposed to be:

base_path="http://mywebsite.com/App/";

Subtle difference but important. After looking carefully at the console logs, I discovered that it was giving a 301 error, which turns out to be a “Permanent Redirect”. The redirect in this case was to the second URI (with the slash attached). Had I been using base_path="http://mywebsite.com/App/index.php"; or any other file name I probably would not have seen this error.

The 301 redirect was, of course, returning “Content-Type: text/html” since it was a plain text page whereas the app was expecting “Content-Type: application/json” and hence the CORS error.

solved Access-Control-Allow-Origin not working with ionic 5 app [closed]