Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax - Callback

Tags:

jquery

ajax

Im making an ajax login form. When a user logs in with the wrong password login.php will return "Incorrect password". But javascript doesn't seem to be picking it up.

$('#login_btn').click(function(){
            var username = $('#login_username').val();
            var password = $('#login_password').val();
            $.post("login.php", { username: username, password: password },
                function(data){
                    if(data == 'Incorrect password'){
                        $('#login_callback').html(data);    
                    }
                    else{
                        $('#login').html(data);
                    }
                }
            );                     

        });

I can login fine, but its just when its the wrong password.

like image 482
Ben Shelock Avatar asked Jan 23 '23 09:01

Ben Shelock


1 Answers

Try changing your request to use $.ajax instead of $.post since $.post silently fails.

$.ajax({
   type: "POST",
   url: "login.php",
   dataType: 'text',
   data: { username: username, password: password },
   success: function(data) {
     if(data == 'Incorrect password'){
       $('#login_callback').html(data);        
     }
     else{
       $('#login').html(data);
     }
   },
   error: function(msg) {
    alert("O NOES");
   }
});

This is based on my theory that login.php returns a non-200 response on failure. Just a hunch though.

like image 121
seth Avatar answered Jan 25 '23 21:01

seth