I am stuck trying to make ajax work on phonegap 2.5.0 application with android. Scanned through stackoverflow and google, but nothing seem to help. Please advice.
So, I am just trying to make a simple AJAX GET request like this:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
var url='http://test.dev/app_dev.php/stuff/add-external?callback=test';
alert('connecting to '+url);
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
crossDomain: true,
contentType: "application/json",
success: function(res) {
alert('success:'+res);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
},
complete: function(data) {
alert('complete');
}
});
}
};
I use jsonp even though it seems not required on phonegap, but helps testing on browser. So this works when I test with browser: alerts "connecting to url", then "success", then "complete". On phonegap however it just alers "conneting to url" and then nothing.
I added <access origin="*" />
to config.xml, and also to res/xml/config/xml. Also added these permissions to AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:windowSoftInputMode="adjustPan" package="com.phonegap.exampleapp" android:versionName="1.1" android:versionCode="1">
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
This is my res/xml/plugins.xml
<?xml version="1.0" encoding="utf-8" ?>
<plugins>
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
</plugins>
Still not working :(. Any help is appreciated. Thanks!
Why not simplify?
$.getJSON('yourtarget.php', function(data) {
alert("my name is: "+data.name+" !!! ");
});
I have just realized that the problem was unrelated to the phonegap. I configured proxy to my computer on the phone's wi-fi settings for testing which works fine from mobile browser, but applications seem to ignore this. Thus, when testing it with external url on web, rather than from my local server it worked as a charm.