I have a mobile web application built using the following versions :-
Using phonegap, this application is built into a native android application and deployed.
In my application, i make various AJAX calls using $.ajax to external websites. For this i employ dataType: 'jsonp' inorder to do the cross domain calling.
When i was testing my app in Chrome v12.0.742.100, everything was working fine and I had no issues with retrieving the data from the external sites. However, as soon as i packaged this into a .apk file and try to run this in the emulator, i find that none of the ajax calls are working.
I have put alerts before and after the ajax call and verified that both alerts are called but the ajax call is as good as ignored. I have logging in both the success callback and the error callback and neither is being reached. I have also verified by puting a breakpoint on the external server website (for my testing, i am just having a separate website on my local machine itself) and the server page is definitely not being called.
In logcat, the error that i can see is this : D/SntpClient( 59): request time failed: java.net.SocketException: Address family not supported by protocol
I am pretty new to phonegap as well as Jquery Mobile but as far as my understanding goes, my phonegap app file is referenced by file:/// protocol whereas my AJAX URL is http://127.0.0.1:someport/someapp/somepage and the error seems to be indicating that these two dont mix!! If this is indeed the case, how do i go about make ajax calls from a phonegap deployed application?
Please feel free to point out anything else that may be helpful! I am pretty stumped at this point.
Edit : I have checked the AndroidManifest.xml file and all the permissions as per this phonegap wiki link are set in this file.
Edit 2 : Adding in my client side code that intitiates the ajax call
var serverUrl = "http://localhost:2424/MyServer/RetrieveMasterData.aspx";
$.ajax({
url: serverUrl,
type: 'POST',
dataType: 'jsonp',
data: { MasterDataID: 1 },
success: function(response) {
...... business logic here
},
error: function(xhr, ajaxOptions, thrownError) {
...... error handling something here
}
});
Android emulator does not recognize "localhost"...it should be 10.0.2.2 instead. try changing the url to http://10.0.2.2:2424/MyServer/RetrieveMasterData.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With