I'm testing Ajax with jQuery but I'm stuck.
The ajax script below displays an alert with the message "Not connect. Verify network" (jqXHR.status === 0)
.
The fact is that the PHP file path specified is definitely correct and that Wamp server is started and works properly.
Looks like it finds the PHP file but it won't execute it (Do I have to set parameters with header()
in the PHP?).
Here is my .js file:
$(document).ready(function () {
$("#connectionButton").click(function () {
var formLogin = $('#login').val();
var formPass = $('#pass').val();
$.ajax({
url: "http://localhost/cordova_test/check_login.php",
type: "POST",
data: {login: formLogin, pass: formPass},
dataType: "html",
succes: function (data) {
alert("Success: " + data);
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg);
}
});
});
});
I use echo in my PHP script to send back the data (the comments are in french but the code is still in English ;) ):
<?php
$formLogin = $_POST['login'];
$formPass = $_POST['pass'];
/* Connexion */
require_once('./connexion.php');
/* Etablissement de la requête */
$sql = $conn->prepare("SELECT * FROM membre WHERE login = :formLogin AND pass = :formPass;");
$sql->execute(array(
'formLogin' => $formLogin,
'formPass' => $formPass
));
/* Récupération des données */
$donnees = $sql->fetchAll(PDO::FETCH_ASSOC);
/* Fermeture de la connexion */
$sql->closeCursor();
/* Vérification et renvoi des données */
if( empty( $donnees ) ) {
echo "INCORRECT_LOGIN";
}
else {
echo $donnees;
}
?>
I've been looking for a solution for 3 days but I really don't get it.