JQuery / Ajax Post a renvoyé des données et une fonction IF

C’est ce qui est dans mon fichier main.js:

$("#login-form").submit( function() { $("#message").removeClass().addClass('messagebox').html( 'Validating....').fadeIn(1000); $.post("login/run", { username : $('#username').val(), password : $('#password').val() }, function(data) { if (data == 'yes') { // add message and change the class of the box and start // fading $("#message").fadeTo( 200, 0.1, function() // start fading the messagebox { $(this).html('Logging In...').addClass( 'messageboxok').fadeTo(900, 1, function() { // redirect to secure page document.location = 'secure.php'; }); }); } else { alert(data); $("#message").fadeTo( 200, 0.1, function() // start fading the messagebox { // add message and change the class of the // box and start fading $(this).html('Your username or password is incorrect') .addClass('messageboxerror') .fadeTo(900, 1); }); } }); return false; } ); 

c’est la fonction à laquelle il fait référence (actuellement défini sur toujours écho oui pour forcer un résultat positif)

 public function run() { echo 'yes'; } 

Le problème que je rencontre est lorsque je fais l’alerte (données), l’alerte indique que la sortie que je reçois est correcte. Cependant, la fonction IF ne fait que la partie ELSE, même si les données sont oui.

L’autre aspect déroutant est que ce code fonctionnait bien avant que je me couche au lit hier soir – ce n’est pas le cas pour le moment.

Y a-t-il un problème dans ma syntaxe n’importe où?


Modifier 1

Modifié par: ajouté , ‘json’ à la fin de la zone $ .post et json_encode ajouté à l’écho

main.js à:

 //login form ajax $("#login-form").submit( function() { $("#msgbox").removeClass().addClass('messagebox').html( 'Validating....').fadeIn(1000); $.post("login/run", { username : $('#username').val(), password : $('#password').val() }, function(data) { if (data == 'yes') { // add message and change the class of the box and start // fading $("#msgbox").fadeTo( 200, 0.1, function() // start fading the messagebox { $(this).html('Logging In...').addClass( 'messageboxok').fadeTo(900, 1, function() { // redirect to secure page document.location = 'secure.php'; }); }); } else { alert(data); $("#msgbox").fadeTo( 200, 0.1, function() // start fading the messagebox { // add message and change the class of the // box and start fading $(this).html('Your username or password is incorrect') .addClass('messageboxerror') .fadeTo(900, 1); }); } }, 'json'); return false; }); //end login form ajax 

et la fonction de:

 public function run() { echo json_encode('yes'); } 

Modifier 2

Modifié en $ .ajax, la fonction renvoyait un tableau codé JSON

main.js:

 $("#login-form").submit( function() { $("#msgbox").removeClass().addClass('messagebox').html( 'Validating....').fadeIn(1000); $.ajax({ url: 'login/run', dataType: 'json', type: 'POST', data: { username : $('#username').val(), password : $('#password').val() }, success: function(data){ alert(data.result); if (data.result == 'yes') { // add message and change the class of the box and start // fading $("#msgbox").fadeTo( 200, 0.1, function() // start fading the messagebox { $(this).html('Logging In...').addClass( 'messageboxok').fadeTo(900, 1, function() { // redirect to secure page document.location = 'secure.php'; }); }); } else { alert(data); $("#msgbox").fadeTo( 200, 0.1, function() // start fading the messagebox { // add message and change the class of the // box and start fading $(this).html('Your username or password is incorrect') .addClass('messageboxerror') .fadeTo(900, 1); }); } //return false; } }); return false; }); 

une fonction:

 public function run() { $result = array('result' => 'yes'); echo json_encode($result); }