I have problems with Access-Control-Allow-Origin at Android 4.1
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.
I read lots of documents but i couldn't find a way to solve this problem.
Run the following command to confirm the origin server returns the Access-Control-Allow-Origin header. Replace example.com with the required origin header. Replace https://www.example.net/video/call/System.generateId.dwr with the URL of the resource that's returning the header error.
The 'Access-Control-Allow-Origin' header is insecure when set to '*' or null, as it allows any domain to perform cross-domain requests and read responses.
Access-Control-Allow-Origin: null This value should not be used to serialize the origin of resources that use a non-hierarchical scheme. Sandboxed documents are defined as null. User agents may grant access to these documents and create a hostile document with null origin.
you need to do something like
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN)
wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
@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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With