Cordova-plugin-ionic-webview: CORS error on android

Created on 8 Nov 2019  路  5Comments  路  Source: ionic-team/cordova-plugin-ionic-webview

Hello. 小an i disable CORS check on android?

"cordova-android": "^8.0.0",
"cordova-plugin-ionic-webview": "^4.1.2",

WebView in io.myapp (74.0.3729.149)

Access to XMLHttpRequest at 'https://replaced-domain.org/...' from origin 'http://localhost' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://www.replaced-domain.org' that is not equal to the supplied origin.

Can I use a non-IonicWebView engine and default?

Most helpful comment

+1 I would like to be able to disable CORS check

All 5 comments

same here. cordova-android:8.1.0 in my earlier app which used cordova-android 7, this problem was not present.

Solved it temporarily by editing the cordova-plugin-ionic-webview\src\android\com\ionicframework\cordova\webview\WebViewLocalServer.java file.

Add the following:

Line 108: tempResponseHeaders.put("Access-Control-Allow-Origin", "*");

Line 262: tempResponseHeaders.put("Access-Control-Allow-Origin", "*");

remove the android platform and add the android platform. After running/compiling, the above java file will be copied from plugins to platforms and the error will go away.

### OR ###

You can do something like this in pure js format:

let imagePath:SafeResourceUrl = this.domSanitizer.bypassSecurityTrustUrl(
   this.di.webview.convertFileSrc(photoURL)
);
let image = imagePath['changingThisBreaksApplicationSecurity'];

set image variable as the src of the img tag.

@gaurav-chandra Thanks!
But after adding the headers all requests stopped running.
net::ERR_INTERNET_DISCONNECTED

No other errors during build or in console.

https://github.com/begbeder/cordova-plugin-ionic-webview/blob/c046d472f9a6a4b5fdfa376c815def4aee8067dc/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java#L106

+1 I would like to be able to disable CORS check

it's not possible to disable CORS on the webview, you have to properly configure the server or use a native http plugin

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KuschL picture KuschL  路  5Comments

paulstelzer picture paulstelzer  路  8Comments

yelhouti picture yelhouti  路  5Comments

lincolnthree picture lincolnthree  路  7Comments

FarhadG picture FarhadG  路  6Comments