Android WebView抛出“仅HTTP支持跨源请求".尝试从磁盘加载资源时发生异常 [英] Android WebView throws "cross origin requests are only supported for http" exception while trying to load resource from disk
问题描述
我用html/js开发了一个单页游戏,并试图将其托管在android webview中.我有一个文件夹src/main/assets/www/
,此行代码可引导我的应用程序:
i have developed a single page game in html/js and am trying to host it inside an android webview. i have a folder src/main/assets/www/
and this line of code to bootstrap my app:
mWebView.loadUrl("file:///android_asset/www/index.html");
index.html
加载一个app.js
文件,这是我的游戏.当我尝试从app.js
内部发出xhr
请求以获取assets/myimage.svg
(物理位置src/main/assets/www/assets/myimage.svg
)时:
the index.html
loads a app.js
file which is my game. when i try to make an xhr
request from within app.js
to get assets/myimage.svg
(physical location src/main/assets/www/assets/myimage.svg
) :
var xhr = new XMLHttpRequest();
xhr.open('get', 'assets/myimage.svg', true);
xhr.send();
我收到此错误:cross origin requests are only supported for http
.为什么这是跨域请求?我该怎么做才能解决此问题?我无法将svg托管在http网络服务器上,也不能内嵌在app.js
中-它必须从磁盘加载.
I get this error: cross origin requests are only supported for http
. why is this a cross-origin request? what can i do to fix this? i cannot host the svg on a http webserver and cannot inline it in app.js
- it has to be loaded from disk.
推荐答案
不确定,但是您可以尝试以下步骤,看看是否有帮助:
Not sure but you can try these steps and see if it helps:
a)初始化您的WebView:
a) Initialize your WebView:
b)获取WebView设置:
b) get WebView settings:
WebSettings settings = _webView.getSettings();
c)设置以下设置:
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
d)现在,您可以通过标准方式加载html文件:
d) now you can load your your html file by standard way:
mWebView.loadUrl("file:///android_asset/www/index.html");
e)不要忘记在清单文件中添加Internet权限:
e) Don't forget to add the internet permission in your manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
这篇关于Android WebView抛出“仅HTTP支持跨源请求".尝试从磁盘加载资源时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!