15

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.

3 Answers 3

52

you need to do something like

if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) 
  wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
4
  • 2
    Thanks, i will try it. But Google API emulators doesn't has this problem; it is interesting. Jul 17, 2012 at 8:12
  • 2
    I don't want to sound foolish, but what file do I add this to in my project? Sep 28, 2012 at 5:31
  • @user1053263 : put it wherever you're creating your WebView, like in an onCreateView or onCreate. You'll probably also do wv.getSettings().setJavaScriptEnabled(true), etc.
    – dokkaebi
    Oct 31, 2012 at 18:37
  • 3
    what to do if I have project under api11 ? Feb 13, 2013 at 10:06
8

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

1
  • 1
    Nice solution. Is there any option to disable this property for below SDK version 16.?
    – Karthick
    Jul 18, 2013 at 12:45
0

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

9
  • Actually they are in the same domain but different subdomains. But i couldn't understand why i got this error only Android 4.1 and not older versions? What changed? Jul 4, 2012 at 5:04
  • Different subdomains are not also allowed. Chrome was acting really wierd ( it was working ok for my "get" request , but was changing my "post" request to "OPTIONS" request ), it took me a lot time to figure out the error. The error was gone when i put the services and client code under the same subdomain.
    – nilgun
    Jul 4, 2012 at 8:13
  • it is impossible for me; because client is mobile device :) thanks for your help. Jul 4, 2012 at 9:53
  • The problem is that if you are serving your service from domainname.com/subdomainname the url your client uses should be the same subdomainname. it is the url not mobile device.
    – nilgun
    Jul 4, 2012 at 10:08
  • i am sorry, i couldn't understand. my service is installed at sub.domain.com and i am trying to connect it from mobile device; but i got error because of origin is null and is not allowed by Access-Control-Allow-Origin. Jul 4, 2012 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.