Reputation: 213
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.
Upvotes: 0
Views: 1638
Reputation: 11721
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.
Upvotes: 2
Reputation: 1266
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);
});
Upvotes: 2