0

I am seeking help for a question I posted on the community phoneGap web site:

http://community.phonegap.com/nitobi/topics/my-phonegap-app-cannot-access-my-web-service?rfm=1

But basically to sum it up I am slowly testing the waters with my phoneGap app, and I want to access a JSON file on my server and parse it and alert it out in my app. So far I cannot get this to work.

Things I have tried: 1) in my config.xml file in my phoneGap files I have the following tag: access origin="http://104.236.6.175" subdomains="true"

2) multiple different ways to access the JSON file with JavaScript, but currently this is my code:

$("#requestJSON").click(function () {
    var xmlhttp = new XMLHttpRequest();
    var url = "http://104.236.6.175/test/test1.json";

    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      var myArr = JSON.parse(xmlhttp.responseText);
      myFunction(myArr);
     }
    }

    xmlhttp.open("GET", url, true);
    xmlhttp.send();
    function myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; i < arr.length; i++) {
    out += '<a href="' + arr[i].url + '">' +
    arr[i].display + '</a><br>';
    }

    alert(out);
    $("#resultLog").html(out);
    }


    });

Any help is much appreciated. Thanks.

Ramona Soderlind
  • 203
  • 2
  • 13
  • Why aren't you using $.ajax or $.get or $.getJSON for your http request? What version of phonegap are you using, have you configured "access origin" in your config.xml? If using latest version, have you included cordova-plugin-whitelist plugin? Have you checked in the logs of your app to see if you have messages? – QuickFix May 28 '15 at 08:07
  • Hi. I never actually got my web service to work. I don't know what I am doing wrong. What I choose to do instead was gave locally stored JSON files on my android phone and parse it out with an Ajax script using JQuery. It seems to be working. – Ramona Soderlind Jun 07 '15 at 17:20
  • Here is an example: http://stackoverflow.com/questions/6417055/download-files-and-store-them-locally-with-phonegap-jquery-mobile-android-and-io – Ramona Soderlind Jun 07 '15 at 17:20

2 Answers2

2

I see you're using jQuery. If you're also using jQuery Mobile for your UI framework, you'll need to call $.mobile.allowCrossDomainPages = true; after you get the deviceready notification. Also you can set the access tag to <access origin="*" /> to allow everything and fine-tune it later.

Also, with jQuery a more concise way to access the return JSON data might be something like this:

$.get("http://104.236.6.175/test/test1.json", function (data, status) {
    var arr = JSON.parse(data);
});
Mike Dailor
  • 1,226
  • 8
  • 16
  • Thank you so much for your time. I tried all of your suggestions and I am still lost. I don't know what I am doing wrong. I feel I have tried to do all I can on the app side. I almost feel like there is something on the server side I am not attending to. Do you have any more ideas? Thanks again. – Ramona Soderlind May 27 '15 at 18:52
2

I had a quick look at your post on phonegap community forum, as I'm gonna be oof for a few weeks before you get a chance to answer my questions I'll just leave you a few hints here.

First, try to use

<access origin="*" />

in your config.xml. It may not be the most secure scenari, but I recommend using * when things don't work and then secure more later by restricting to the domain you need.

In latest versions of cordova/phonegap you need to have the plugin "cordova-plugin-whitelist" to be able to access remote server. Without this, all communications are cut.

If I don't mistake, you should add this line in config.xml to add the plugin (I don't use build anymore so I can't check):

<gap:plugin name="cordova-plugin-whitelist" source="npm"/>

More info about this change in latest cordova/phonegap here and here or here.

And finally, to get more information about what's happening it's usefull to access the logs. You could do that with weinre by clicking the debug checkbox in phonegap build, or by using adb logcat on your computer (need to allow usb debuging on your device and maybe install drivers on your computer). If you do so there are chances that you'll get a nice error message saying you what's going on.

QuickFix
  • 11,661
  • 2
  • 38
  • 50