XMLHttpRequest cannot load file from android asset folder on emulator

吃可爱长大的小学妹 提交于 2019-11-30 15:26:29

问题


I am new to hybrid development. I wrote a small application which launches webview. I have XML, JS files copied in /asset folder.

App works fine on my samsung tablet but I get following errors on emulator

05-30 06:09:07.080: I/chromium(1245): [INFO:CONSOLE(0)] "XMLHttpRequest cannot load file:///android_asset/resource/service_config.xml. Cross origin requests are only supported for HTTP.", source: file:///android_asset/Startup.html (0)

I got to know it happens due to chrome browsers security model and android webview too uses the same component as chrome browser. But all these largely related to chrome browser not addressing the issue on emulator.

Appreciate any help on this issue.

Thanks,
iuq


回答1:


got the same error. I fixed it by changing some settings of the webview in the activities onCreate() method as follows:

// settings for webview
mWebView = (WebView)findViewById(R.id.activity_main_webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);

//load file
mWebView.loadUrl("file:///android_asset/www/index.html");

Hope that might help you :)



来源:https://stackoverflow.com/questions/23955050/xmlhttprequest-cannot-load-file-from-android-asset-folder-on-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!