0

I would like to create a web app with phonegap (I'm trying on Android emulator), but I have a problem with the "same domain policy": is there any way to disable this restriction. I need to load html/json data from an external server (not my own, so I can't modify it), but when I try to get data JQuery returns an undefined object. Here's my code:

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Title</title>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript">
            $.ajaxSetup ({
                cache: false
            });
            var ajax_load = "<img src='img/load.gif' alt='loading...' />";
            var jsonUrl = "external url";
                $("#result").html(ajax_load);
                $.getJSON(jsonUrl, function(json) {
                    var result = json.responseData;
                    $("#data").html("Result: " + result);
                });
        </script>
    </head>
    <body>
        <div id="data"></div>
    </body>
</html>

PhoneGapTestActivity

public class PhoneGapTestActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}

Searching online and reading other questions, I tried to set the whitelist in the file phonegap.xml

<?xml version="1.0" encoding="UTF-8"?>
<phonegap>
    <access origin="*" subdomains="true" />
</phonegap>

But what I got is: "Result: undefined". Thanks a lot!

EDIT: After many tries, I noticed that the request works (I received the data), but there was some issues while accessing json data contents, but now it works!. Thanks for all answers.

Paco
  • 468
  • 5
  • 10
  • Have you checked out this similar question already? http://stackoverflow.com/questions/7154988/will-jquery-load-work-on-phonegap – Jon May 15 '12 at 16:40

3 Answers3

0

There is no reason why it shouldn't work. I'm not a big user of jQuery but there are some things you can set to enable CORS.

A CORS POST request works from plain javascript, but why not with jQuery?

Community
  • 1
  • 1
Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
0

Try this.. i dont know if its working in this case, but it working for me.

<?xml version="1.0" encoding="utf-8"?>
<phonegap>
<access origin="http://127.0.0.1*"/>
<log level="DEBUG"/>
</phonegap>

Sunil Dodiya
  • 2,605
  • 2
  • 18
  • 21
0

After many tries, I noticed that the request works (I received the data), but there was some issues while accessing json data contents.

Paco
  • 468
  • 5
  • 10