WebView Javascript 从本地 HTML 文件跨域

发布于 2024-12-22 20:25:03 字数 203 浏览 5 评论 0原文

我将本地 html 文件(从资产文件夹)加载到应用程序 WebView。 在 HTML 中,我运行 jQuery.getJSON(url)。 url 是远程服务器。

此操作失败,我猜测是因为不同的来源问题(跨域)。我在 chrome 上运行相同的文件,它特别这么说。

有没有办法让Android中的WebView从远程服务器加载数据到本地加载的HTML文件上?

I load a local html file (from assets folder) to the app WebView.
In the HTML I run a jQuery.getJSON(url). the url is a remote server.

This action fails, and I'm guessing because of a different origin issue (cross domain). I run the same file on chrome and there it specifically says so.

Is there a way to allow the WebView in Android to load data from remote server on a local loaded HTML file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮光沉寂 2024-12-29 20:25:03

今天早上我找到了似乎有效的解决方案。

Java部分

初始化您的WebView:

WebView _webView = (WebView) this.findViewById(R.id.id_of_your_webview_in_layout);

获取WebView设置:

WebSettings settings = _webView.getSettings();

设置以下设置:

settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true); //Maybe you don't need this rule
settings.setAllowUniversalAccessFromFileURLs(true);

现在您可以通过标准方式加载您的html文件:

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

Javascript部分

通过标准方式创建XHR请求

var xhr = new XMLHttpRequest();
xhr.open("get", "http://google.com", false);
xhr.send();

在某处打印结果

document.body.innerHTML = xhr.responseText

注意:
此过程仅适用于 API 级别 16 或更高级别(至少文档是这么说的)。

Today morning I found solution that seems to be working.

The Java part

Initialize your WebView:

WebView _webView = (WebView) this.findViewById(R.id.id_of_your_webview_in_layout);

get WebView settings:

WebSettings settings = _webView.getSettings();

set following settings:

settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true); //Maybe you don't need this rule
settings.setAllowUniversalAccessFromFileURLs(true);

now you can load your your html file by standard way:

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

The Javascript part

Create XHR request by standard way

var xhr = new XMLHttpRequest();
xhr.open("get", "http://google.com", false);
xhr.send();

Print the result somewhere

document.body.innerHTML = xhr.responseText

NOTICE:
This procedure works only on API level 16 or higher (At least the documentation says that).

最丧也最甜 2024-12-29 20:25:03

不要忘记在清单文件中添加互联网权限:

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

还要确保您使用的是 JSONP 请求(不要忘记 &callback=? ,如上所述)

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

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

Also make sure you are using JSONP requests (don't forget the &callback=? as stated above)

堇年纸鸢 2024-12-29 20:25:03

我将本地 html 文件(从资产文件夹)加载到应用程序 WebView

请注意,您没有说明如何执行此操作。我猜测它是通过 file:///android_asset URL 上的 loadUrl() 实现的。

有没有办法让Android中的WebView从远程服务器加载数据到本地加载的HTML文件上?

尝试使用 loadDataWithBaseURL() 加载内容,并提供远程服务器上的 URL 作为基本 URL。

I load a local html file (from assets folder) to the app WebView

Note that you failed to say how you are doing this. I am going to guess that it was by a loadUrl() on a file:///android_asset URL.

Is there a way to allow the WebView in Android to load data from remote server on a local loaded HTML file?

Try using loadDataWithBaseURL() to load the content, supplying a URL on the remote server as the base URL.

皓月长歌 2024-12-29 20:25:03

Ajax 调用无法从本地文件系统进行。超过了就会变成跨域。所以它不会起作用。它在 eclipse 中工作,因为你从本地服务器尝试过。

Ajax calls wont work from local file system. More over it will become cross-domain. So it wont work. It worked in eclipse, becuz you tried from local server.

浪菊怪哟 2024-12-29 20:25:03

我们使用的解决方案是使用Android从服务器获取更新文件,将它们放在web文件夹中并覆盖文件,然后浏览。

A solution we used was to use Android to get the update files from the server, place them and overwrite files in the web folder, and then browse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文