I have developed an application that loads an entire website in an Android WebView. The native code in the Android project communicates with the webpage using the Android Jockey Library, and vice versa.
Everything is working smoothly except for one instance where I encounter the error:
XMLHttpRequest cannot load ... Origin <url> is not allowed by Access-Control-Allow-Origin
. This occurs when the website loaded in the WebView makes an ajax call to the backend API.
My website is hosted at m.mywebsite.com
which makes ajax calls to api.mywebsite.com
, both are separate sub-domains. Interestingly, this setup works fine on iOS.
I am currently testing this on Android v4.4.2. I have tried compiling against target SDK 15 and 19 but it didn't make any difference.
I have configured my WebView with the following settings:
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAppCacheEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
}
webView.getSettings().setSupportZoom(false);
webView.setWebChromeClient(new WebChromeClient());
webView.setInitialScale(0);
And here is how I integrated Jockey:
if (Utils.isNetworkAvailable(this)) {
jockey = JockeyImpl.getDefault();
jockey.configure(webView);
webViewClient = new MyWebViewClient(this);
jockey.setWebViewClient(webViewClient);
setJockeyEvents();
webView.loadUrl(EnvironmentManager.getStartUpURL());
}
Has anyone else encountered a similar issue or found a solution to this problem?
Thank you!