WebView Javascript来自本地HTML文件的跨域

时间:2022-08-23 10:20:11

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.

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

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.

这个动作失败了,我猜是因为一个不同的起源问题(跨域)。我在chrome上运行相同的文件,并在那里专门说明。

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

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

5 个解决方案

#1


53  

Today morning I found solution that seems to be working.

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

The Java part

Initialize your WebView:

初始化WebView:

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

get WebView settings:

获取WebView设置:

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:

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

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

The Javascript part

Create XHR request by standard way

通过标准方式创建XHR请求

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).

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

#2


2  

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

不要忘记在清单文件中添加Internet权限:

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

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

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

#3


0  

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

我将本地html文件(从assets文件夹)加载到应用程序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.

请注意,您没有说出您是如何做到这一点的。我猜测它是由一个文件:/// android_asset URL上的loadUrl()。

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

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

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

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

#4


0  

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.

Ajax调用不能从本地文件系统工作。更多内容将成为跨域。所以它不会工作。它在eclipse中工作,因为你试过从本地服务器。

#5


0  

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.

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

#1


53  

Today morning I found solution that seems to be working.

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

The Java part

Initialize your WebView:

初始化WebView:

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

get WebView settings:

获取WebView设置:

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:

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

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

The Javascript part

Create XHR request by standard way

通过标准方式创建XHR请求

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).

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

#2


2  

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

不要忘记在清单文件中添加Internet权限:

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

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

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

#3


0  

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

我将本地html文件(从assets文件夹)加载到应用程序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.

请注意,您没有说出您是如何做到这一点的。我猜测它是由一个文件:/// android_asset URL上的loadUrl()。

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

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

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

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

#4


0  

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.

Ajax调用不能从本地文件系统工作。更多内容将成为跨域。所以它不会工作。它在eclipse中工作,因为你试过从本地服务器。

#5


0  

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.

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