I am developing a mobile app using cordova, and I am facing a problem for now 2 days I can't resolve, I am trying to load a simple php file hosted on a server :
<?php
echo 'Salut'
?>
And I am trying to read the content while using an ajax call :
$.ajax({
type: 'get',
url: 'https://mydomain.so/index.php',
dataType: 'text',
success: function (data) { alert(data); },
error: function (x, e) {
if (x.status == 0) {
alert('You are offline!!
Please Check Your Network.');
} else if (x.status == 404) {
alert('Requested URL not found.');
} else if (x.status == 500) {
alert('Internel Server Error.');
} else if (e == 'parsererror') {
alert('Error.
Parsing JSON Request failed.');
} else if (e == 'timeout') {
alert('Request Time out.');
} else {
alert('Unknow Error.
' + x.responseText);
}
}
});
But the text show with alert is "You are offline", I do not understand because on the same html page there is a video youtube that I can watch. And I do not know why it's telling me I am offline while I can watch the video... Someone know why I can't read php file but I can watch youtube video ?
Edit: Now I can read my php file inside of my onDeviceReady() function, but if I try to to do while clicking a button, it shows x.status==0, I tried to add :
header('Access-Control-Allow-Origin: *');
in my php file but it's not working :s
Edit2: It's working fine, I was changing the location outside of the success function, I put it inside and it's working perfectly. So I had to add https in my meta tag(before data:) and set new location inside of the ajax request.
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https: data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
It could be possible to get a status code of 0 if you have sent an AJAX call and a refresh of the page was triggered before getting the AJAX response. The AJAX call will be cancelled and you will get this status.
or
If you are calling ajax using submit button or on anchor tag, you probably prevent the default behaviour of submit or anchor tag.
If it is related CORS add following line in your php file.
header('Access-Control-Allow-Origin: *');
Hope it helps you