-1

whenever I run "ionic cordova run android" and deploy the apk in the real device, I got error message: Access to XMLHttpRequest at 'http://XXXX/mobile/data/1/XXXX' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

(this works in the ionic simulator by adding the proxy in config file, but not the real device).

Does anyone face this problem as well? here is my ionic info: ionic (Ionic CLI) : 4.2.1 (/usr/local/lib/node_modules/ionic) Ionic Framework : ionic-angular 3.9.2 @ionic/app-scripts : 3.2.0

Cordova:

cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1) Cordova Platforms : android 7.1.2 Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.2.3, (and 8 other plugins)

System:

NodeJS : v8.12.0 (/usr/local/bin/node) npm : 6.4.1 OS : macOS High Sierra

  • Here is the detail about the CORS issue https://blog.ionicframework.com/wkwebview-for-all-a-new-webview-for-ionic/ – Kabir Nov 22 '18 at 18:41

3 Answers3

0

The backend has to enable other applications that access the API, this is done by enabling CORS.

In development you can use this plugin in chrome:

Allow-Control-Allow-Origin
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi 

More in production the backend should fix that BUG..

0

This is a server-side problem, you have to activate the CORS in the following link you will find the necessary configuration for your server.

gabfiocchi
  • 190
  • 11
0

More than 4 years since this questions was created.

I had the same problem this last weeekend and at the end, today found my solution:

This was my error, similar as yours: access to XMLHttpRequest at 'https://myhost.com/api/myapi' from origin 'https://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

When i was on class years ago, we did an android app with a backend, on it, we only allow localhost requests, so i try to add it to my server CORS, so i did this:

string[] hosts = new string[ 4 ] {
    "https://mihost.com", 
    "https://localhost", 
    "http://localhost" 
};

app.UseCors( cors => 
    cors.WithOrigins( host )
    .AllowAnyHeader( )
    .AllowAnyMethod( ) 
);

NET Core 6 code

And then i'm able to make request between android device and api, and also web request.

SrAlbert
  • 1
  • 2