I'm currently developing a project using PhoneGap and I need to do an ajax request to my local webserver which has already:
PHP code
header('Access-Control-Allow-Origin: *');
Anyway, when I do an ajax request with jQuery on Android I get this error (in adb logcat):
D/chromium(23078): Unknown chromium error: 0
The Javascript code is:
$.ajax({url:"http://192.168.1.219/works/privati/folder/api.php/getlastmaginfo",dataType:"json",success:function(data)
{
console.log("Finished loading by ajax");
console.log(data);
}});
In Ripple Emulator works as expected, in Android nope.
Any suggestion? Thank you for the help!
UPDATE 2013-08-21:
After some researches I came at the conclusion that $.ajax won't work with PhoneGap (don't know why, maybe a bug?).
We must use $.get instead, but when I do a request with $.get I get Unknown chromium error: -6
I also read here that the problem is due to an Android's bug with the WebView URL mechanism.
I'll continue my research until I find a good and working solution
UPDATE 2013-08-21 (2): Not even using this works...
var fileTransfer = new FileTransfer();
fileTransfer.download(
"http://192.168.1.219/works/privati/qlipmag/api.php/getlastmaginfo",
"json.json",
function(entry) {
console.log("OKAY");
},
function(error) {
console.log(error);
}
);
Error is at new FileTransfer();
=> Uncaught ReferenceError: FileTransfer is not defined
UPDATE 2013-08-22:
It doens't work even by loading an external image:
<img src="http://externalhost.com/image.jpg"/>
Same error.
In android manifest file I already setted permission:
<uses-permission android:name="android.permission.INTERNET" />
I don't really know from where comes the problem...
UPDATE 2013-08-27: I tried the same code on PhoneGap for iPhone (in iPhone Emulator) and the ajax request was successfull when I used an external website. Using an external website also on android doesn't get the same result. Same error.
Anybody can see why? Access origin are setted fine...
UPDATE 2013-08-27 (after 1 hour)
ISSUE SOLVED
Actually PhoneGap doesn't allows ajax request to ip addresses, it only allows requests to whitelisted (in config.xml) domains. I used the production server (with domain) to test and it worked.
Thank you anyway guys. I hope this can help
To clarify on this in case someone stumbles upon it like I did, $.ajax does work with PhoneGap/Cordova for sure - but as you found .. the domain must be whitelisted.
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