WebView Javascript 从本地 HTML 文件跨域
我将本地 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
今天早上我找到了似乎有效的解决方案。
Java部分
初始化您的WebView:
获取WebView设置:
设置以下设置:
现在您可以通过标准方式加载您的html文件:
Javascript部分
通过标准方式创建XHR请求
在某处打印结果
Today morning I found solution that seems to be working.
The Java part
Initialize your WebView:
get WebView settings:
set following settings:
now you can load your your html file by standard way:
The Javascript part
Create XHR request by standard way
Print the result somewhere
不要忘记在清单文件中添加互联网权限:
还要确保您使用的是 JSONP 请求(不要忘记 &callback=? ,如上所述)
Don't forget to add the internet permission in your manifest file:
Also make sure you are using JSONP requests (don't forget the &callback=? as stated above)
请注意,您没有说明如何执行此操作。我猜测它是通过
file:///android_asset
URL 上的loadUrl()
实现的。尝试使用
loadDataWithBaseURL()
加载内容,并提供远程服务器上的 URL 作为基本 URL。Note that you failed to say how you are doing this. I am going to guess that it was by a
loadUrl()
on afile:///android_asset
URL.Try using
loadDataWithBaseURL()
to load the content, supplying a URL on the remote server as the base URL.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.
我们使用的解决方案是使用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.