I want to issue an AJAX call to my remote server, upload a few parameters and download some content. This script works perfectly in a browser, but doesn't work with PhoneGap (testing on Ripple, chrome).
I have looked at problems and solutions such as:
PhoneGap not issuing AJAX (jsonp) request
Issues with jQuery, ajax, and jsonp
Ajax call:
$.ajax({
url: "https://mydomain.com/file.php",
data: {test:"test"},
type: 'GET',
dataType: 'jsonp',
timeout: 5000,
success: function(data){
if(data['var'] == 'correct'){
notifyAlert('correct','Login');
}else{
notifyAlert('incorrect','Login');
}
},
error: function(x,e){
if(x.status==0){
notifyAlert('You are offline!!\n Please Check Your Network.', 'Error');
}else if(x.status==404){
notifyAlert('Requested URL not found.', 'Error');
}else if(x.status==500){
notifyAlert('Internel Server Error.', 'Error');
}else if(e=='parsererror'){
notifyAlert('Error.\nParsing JSON Request failed. '+x.status, 'Error');
}else if(e=='timeout'){
notifyAlert('Request Time out.');
}else {
notifyAlert('Unknow Error.\n'+x.responseText, 'Error');
}
}
});
Remote server code:
$record = array('var' => 'correct');
echo $_GET['callback'] . '(' . json_encode($record) . ');';
My config.xml file has:
<access origin="https://mydomain.com" />
When testing on Ripple (emulator) it gives me the status 0 error. When testing te same script on my local server (through the browser) it gives me the correct var alert.
I also tested a json call and this works perfectly in the emulator but it doesn't work when I upload the apk to my phone. Same result as the problem above with jsonp (status 0 error).
Finally found the problem. It was a problem with my SSL certificate. I tested on a server with a unsigned certificate which gave me the incorrect response. Using a signed certificate resolved all my problems..