7

I am getting this error :

XMLHttpRequest cannot load http://192.168.1.33:8080/ws/target. Origin null is not allowed by Access-Control-Allow-Origin.:1

When i am trying to load a html page on webview .I enter the script tag like that.

<script src="http://192.168.1.33:8080/target/target-script-min.js#anonymous"></script>

Actually This error coming when I am trying to debug my code on weinre . can you please tell how to remove this error ?

is there way to remove this error ? But when I make phonegap project and load same HTML with same import script line.

<script src="http://192.168.1.33:8080/target/target-script-min.js#anonymous"></script>

with this line

I am able to debug my file on winre.

can you please tell how i will remove this error when I load on webview ?

  • Did you check this thread : http://stackoverflow.com/questions/11318703/access-control-allow-origin-error-at-android-4-1 ? – mguimard Aug 04 '14 at 12:14
  • You have to enable CORS in the web-service, and allow the client IP (or *) to Access-Control-Allow-Origin in the web-service. – Avishek Aug 04 '14 at 12:15
  • what is does ? it solve my problem ?> –  Aug 04 '14 at 12:15

1 Answers1

13

The issue is occurring because of the CORS policy in your browser. In order to remove the error you normally have to make a change server side to whatever server you happen to be using. You'll have to add the header:

Access-Control-Allow-Origin: *

See here for more info on how to do this for specific back-end framework.

That said since your using cordova you can also override the browser's behavior like so:

if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
    wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
}

if you are using a webview. If you don't want to mess with the webview directly you can also set

<access uri="*" subdomains="true" />

In your res/xml/cordova.xml. See here for more details http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html

Matt Lacey
  • 8,227
  • 35
  • 58
ShaneQful
  • 2,140
  • 1
  • 16
  • 22
  • I am not using phonegap.i am just opening the html in webview.so i don't have conf file –  Aug 04 '14 at 14:14
  • Sorry one of the tags was codova i.e. phone gap. The first and second solutions I proposed should still work fine. In the second `wv` is your `WebView` – ShaneQful Aug 04 '14 at 14:45
  • In fist solution where i will write this Access-Control-Allow-Origin: * ?? –  Aug 04 '14 at 15:12
  • id this your second solution if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) wv.getSettings().setAllowUniversalAccessFromFileURLs(true);? –  Aug 04 '14 at 15:12
  • The first solution you do on your server, that is to say on `http://192.168.1.33:8080/`, you'll need to add it as a header on the request for the file e.g. if your using an apache server follow this guide (http://enable-cors.org/server_apache.html). The link in the answer will show you how to do it for your setup. – ShaneQful Aug 04 '14 at 16:46
  • In regards to your question about the second answer, I'm not sure what exactly you mean but this code is to be used on your WebView object in android. If your creating the WebView in code you should already have access to it to run the above code on it. If you specified it in xml you can use `final WebView wv = (WebView)this.findViewById(R.id.yourwebviewsid);` inside your `onCreate` in order to access it. – ShaneQful Aug 04 '14 at 16:47
  • i wanna use the first solution you've suggested but for a specific domain (for safety reasons). the problem is that the my android app webview domain is "file:\\". any idea how to solve this issue ? i've opened a stack overflow question here: http://stackoverflow.com/questions/34046811/allowing-android-webview-iframing-my-web-site-specificly – danfromisrael Dec 02 '15 at 15:47