Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access-Control-Allow-Origin Error At Android 4.1

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.

like image 483
bahadir arslan Avatar asked Jul 03 '12 20:07

bahadir arslan


People also ask

How do I fix Access-Control allow Origin error?

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.

Is Access-Control allow Origin * Insecure?

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.

What is Access-Control allow origin null?

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.


2 Answers

you need to do something like

if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) 
  wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
like image 163
slushi Avatar answered Sep 26 '22 07:09

slushi


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

like image 27
johnnybgoode Avatar answered Sep 23 '22 07:09

johnnybgoode