Reputation: 237
I'm developing an android application using cordova and therefore I also need AJAX requests to happen. I am using jQuery for the requests and after updating the index.php security policies I made it to connect my app to a remote server. But now I receive a error 404 - Not found when starting a AJAX request. Could the error be related to the data I POST to the server? The url is correct and I have enabled the whitelist plugin.
EDIT:
function ajaxrequestlogin() {
var login_username=$("#username").val();
var login_password=$("#password").val();
var dataString="username="+login_username+"&password="+login_password;
$.ajax({
type: "POST",
url: "http://www.my-url.de/path/to/myapi.php",
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(){ $("#message").html('Verbinde...');},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);},
success: function(data){
if(data=="success") {
window.localStorage.setItem(loggedin, true);
window.localStorage.setItem(username, login_username);
window.localStorage.setItem(password, login_password);
alert("Erfolgreich eingeloggt als "+login_username);
}
else if(data=="wrong_userorpass") {
alert("Username und Passwort stimmen nicht überein!");
}
else if(data=="failed") {
alert("Ups! Ein Fehler ist aufgetreten. Bitte versuche es erneut.");
}
}
});
return false;
}
The application should send username and password to the server which authenticates the credentials and returns either success or wrong_userorpass by using
echo "success";
for example. Do I have to change something in my API's PHP script to make the option no-cache work or should it work fine without any intervention?
Upvotes: 1
Views: 2140
Reputation: 237
Okay after some hours of trying around, changing security policies and config.xml I finally solved the problem so I'll just share it to you 'cause I think it's pretty a common problem when trying to use Ajax with cordova applications.
While trying to fix the problem I read like 10 forum posts of people who have updated their cordova and thereafter Ajax dropped 404 errors. The solution for those guys was always to just install the cordova-plugin-whitelist
plugin. For me (I downloaded version 6.3) this plugin was already installed and activated so I did not bother about those answers. After trying around this evening I realized that everything was ok with my scripts and it had to be a cordova specific issue (I tested it with a browser version of the ajax request) so I just typed
cordova plugin add cordova-plugin-whitelist
.
It installed (even the plugin was already installed and active) and immediately thereafter everything worked as it should.
So even if you think the whitelist-plugin cannot be the problem: just try install it again and it should work fine.
Upvotes: 12