Android WebView 抛出“跨域请求仅支持http";尝试从磁盘加载资源时出现异常 [英] Android WebView throws "cross origin requests are only supported for http" exception while trying to load resource from disk

查看:30
本文介绍了Android WebView 抛出“跨域请求仅支持http";尝试从磁盘加载资源时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 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.为什么这是一个跨域请求?我能做些什么来解决这个问题?我无法在 http 网络服务器上托管 svg,也无法在 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) 不要忘记在清单文件中添加互联网权限:

e) Don't forget to add the internet permission in your manifest file:

<uses-permission android:name="android.permission.INTERNET"/>

这篇关于Android WebView 抛出“跨域请求仅支持http";尝试从磁盘加载资源时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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