You are here

Error "Access-Control-Allow-Origin" At Android 4.1

One of my Android Hybrid HTML5 app does not work now at Android 4.1.0, but it worked fine on 3.0 and 4.0.

Issue:
The app call one WFS service from other server, this Ajax throw exception:

11-22 14:30:47.062: E/Web Console(734): XMLHttpRequest cannot load http://xxxx Origin null is not allowed by Access-Control-Allow-Origin. at null:1

Solution:

if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) //16
// yourwebview, i use phonegap here
super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true);

Why:
From 4.1.0, the XMLHttpRequest permission from local file to remote service is set to be false as default.

By the way, from Android 3.0, you may got problem to load remote server content with authenication via Ajax in webview, try to add 80 port in you url.

public void onCreate(Bundle savedInstanceState)
{
......
AuthenticationToken authenticationToken = new AuthenticationToken();
authenticationToken.setUserName("xxx");
authenticationToken.setPassword("xxxx");
super.setAuthenticationToken(authenticationToken, "dev.myservice.net:80", "test");

Ref:
http://stackoverflow.com/questions/11318703/access-control-allow-origin-...