I'm facing an issue with my android webview as it loads a website locally stored in my assets. The problem arises when certain parts of the website use jquery $.ajax-gets to fetch HTML to display in a modal, resulting in cross-domain problems. Despite extensive research and troubleshooting, including checking the manifest file for necessary permissions, I haven't been able to resolve this issue.
Running on a Nexus 7, the files are situated in the assets folder (file:///android_asset
). While everything else functions correctly, the ajax GET requests continue to pose a challenge.
The relevant code in the manifest includes:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Key webview settings include:
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setSupportMultipleWindows(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mWebView.addJavascriptInterface(this, "android");
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
mWebView.loadUrl("file:///android_asset/site/index.html");
}
Regarding the javascript code:
var load = function ( source, callback, dontShowLoader ) {
if( !dontShowLoader ) {
loading( 'show' );
}
$.ajax({
url: source,
type: 'GET',
data: {
campaign: true
},
success: function ( data ) {
var $data = $(data);
loading( 'hide' );
$data.data( 'url', source );
callback( $(data) );
}
});
};
Is there something crucial that I might be overlooking? Is it truly impossible to perform ajax GETs over local file content? Keep in mind that I only have access to the local files, as the application is often used offline without an internet connection.