XMLHttpRequest无法从模拟器上的android资产文件夹加载文件 [英] XMLHttpRequest cannot load file from android asset folder on emulator

查看:171
本文介绍了XMLHttpRequest无法从模拟器上的android资产文件夹加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是混合动力开发的新手.我编写了一个启动Webview的小应用程序.我在/asset文件夹中复制了XML,JS文件.

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

App在我的三星平板电脑上运行正常,但在模拟器上出现以下错误

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

05-30 06:09:07.080:I/铬(1245):[INFO:控制台(0)] "XMLHttpRequest无法加载 文件:///android_asset/resource/service_config.xml.交叉原点 请求仅支持HTTP.",来源: file:///android_asset/Startup.html(0)

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)

我知道这是由于chrome浏览器的安全模型所致,而android webview也使用了与chrome浏览器相同的组件.但是所有这些都与chrome浏览器有关,无法解决模拟器上的问题.

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.

谢谢,
iuq

Thanks,
iuq

推荐答案

出现相同的错误.我通过更改活动onCreate()方法中的webview的某些设置来修复它,如下所示:

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");

希望对您有所帮助:)

这篇关于XMLHttpRequest无法从模拟器上的android资产文件夹加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆