Android 4.1 上的 Access-Control-Allow-Origin 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11318703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access-Control-Allow-Origin Error At Android 4.1
提问by bahadir arslan
I have problems with Access-Control-Allow-Origin at Android 4.1
我在 Android 4.1 上遇到 Access-Control-Allow-Origin 问题
In my application i have some local HTML files and Javascripts which i was using to fetch data from web services. Until trying Android 4.1 there was no problem but after trying at Android 4.1 i got this error.
在我的应用程序中,我有一些本地 HTML 文件和 Javascript,用于从 Web 服务获取数据。在尝试使用 Android 4.1 之前没有问题,但在尝试使用 Android 4.1 后出现此错误。
I read lots of documents but i couldn't find a way to solve this problem.
我阅读了很多文档,但找不到解决此问题的方法。
回答by slushi
you need to do something like
你需要做类似的事情
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN)
wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
回答by johnnybgoode
@I am Developer and the others who are facing the same problem.
@我是开发人员和其他面临同样问题的人。
Slushis solution works fine. But if you want to compile against and support systems below API11 you have to add the following:
Slushis 解决方案工作正常。但是如果你想编译并支持 API11 以下的系统,你必须添加以下内容:
if (Build.VERSION.SDK_INT >= 16) {
Class<?> clazz = webView.getSettings().getClass();
Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);
if (method != null) {
method.invoke(webView.getSettings(), true);
}
}
This will load and invoke the method at runtime, so you can compile with e.g. Android 2.3.3.
这将在运行时加载并调用该方法,因此您可以使用例如 Android 2.3.3 进行编译。
回答by nilgun
Are your web services hosting from the same domain ? I used to get this error while making an ajax call to a service under a different domain. If you have control on the web service, you can set Access-Control-Allow-Origin: * in the header, (although this way is not a secure way of doing so.)
您的网络服务是否来自同一个域?我曾经在对不同域下的服务进行 ajax 调用时遇到此错误。如果您可以控制 Web 服务,则可以在标题中设置 Access-Control-Allow-Origin: *,(尽管这种方式不是一种安全的方式。)