3

I'm fairly new to jQuery. I've written this script that manages an ajax request for a login.

My problem is sometimes the script sometimes does not execute anything from if (data == "success") (or any other login response) if I log in and out a few times.

It stalls at $.post("exec_login.php") even though it ran through the code previously with no problems without reloading the page.

$(document).ready(function() {

    //HTTP AJAX LOGIN REQUEST
    $("#modal_login_button_existing_user").click(function(){

        $.post("exec_login.php",
        {
            email : $("#modal_login_existing_email").val(),
            password : $("#modal_login_existing_password").val()
        },
        function(data,status){
            if (data == "success")
            {
                $('#modal_login_alert_success').html('').append('<strong>Welcome!</strong> You are now successfully logged in.').fadeIn(1000).delay(2000).queue(function(){ 
                    $('.session_hide').hide();
                    $('#modal_login').modal('hide').delay(1000).queue(function(){ 
                        $('.session_show').show();
                        $('.session_fadein').fadeIn(2000);
                        $('.session_opacity').css({opacity: 1}).show();

                        //$('.logged_in_name').html('').append(first + ' ' + second).show();
                    });
                });
            }
            else if (data == "failure")
            {   
                $('#modal_login_alert_success').hide();
                $('#modal_login_alert_failure').html('').append('<strong>Woops!</strong> The login details you provided were incorrect.').show();
            }
            else if (data == "empty")
            {
                $('#modal_login_alert_success').hide();
                $('#modal_login_alert_failure').html('').append('<strong>Darn!</strong> Some of the login boxes are empty still.').show();
            }
        });
    }); 
});

and log out code:

$(document).ready(function() {  

    //HTTP AJAX LOGOUT REQUEST
    $("#modal_logout_button").click(function(){
        alert("test");
        $.post("exec_logout.php",
        {
            logout : "1",
        },
        function(data,status){
            if (data == "success")
            {
                $('.session_hide').fadeIn(1000).delay(200).queue(function(){
                    $('.session_show').hide();
                    $('.session_fadein').hide();
                    $('.session_opacity').hide();
                    $('#modal_logout').modal('hide');
                });
            }
        });
    }); 
});
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Amy Neville
  • 10,067
  • 13
  • 58
  • 94
  • Can you extrapolate on what happens when it "acts weirdly" please? – Travis J Jun 09 '13 at 19:27
  • Ah, it does not execute anything from if (data == "success") – Amy Neville Jun 09 '13 at 19:27
  • Do any errors appear in the browser's console? Check the Net (or Network) tab as well. – Andy G Jun 09 '13 at 19:28
  • @AmyNeville did you check to see if the server response is always right? – Spokey Jun 09 '13 at 19:29
  • @AmyNeville - So this is an ajax login/logout, I am assuming the page does not refresh. Can you show the code for logging out, or any relevant parts? – Travis J Jun 09 '13 at 19:29
  • It happens when I cycle round. If I log in, log out and then try to log in again thats when it will freeze with the login modal still up. – Amy Neville Jun 09 '13 at 19:30
  • Yes, added logout code – Amy Neville Jun 09 '13 at 19:31
  • please show the code of `exec_login.php` – ianace Jun 09 '13 at 19:32
  • The weird thing is that when it freezes it DOES log me in, it just gets stuck – Amy Neville Jun 09 '13 at 19:32
  • 1
    have a look at this technique http://stackoverflow.com/questions/1820927/request-monitoring-in-chrome and check if the server response is correct. I suspect the problem is not your javascript... – Pevara Jun 09 '13 at 19:33
  • I know it replied with "success" because it logged me in ianace – Amy Neville Jun 09 '13 at 19:35
  • "logged in"? how do you know that? try doing a `console.log(data.status)` inside `if (data == "success")` – ianace Jun 09 '13 at 19:37
  • Did you try `if ( $.trim(data) == "success")` ? – adeneo Jun 09 '13 at 19:37
  • I put alert(data + " " + status) after function(data,status){ and it replied with "success success" so the correct message is definitely coming back from the login php file – Amy Neville Jun 09 '13 at 19:49
  • It is not because you get logged in that the server replies with a valid answer. Perhaps your script failed somewhere after the login procedure. I strongly advise you to inspect the result, it is always my first option whenever I have ajax issues. – Pevara Jun 09 '13 at 19:54

1 Answers1

2

I took the approach of simplifying my script to the following for the success response. Unsprisingly working like a nicely oiled machine now, if a bit less of a flashy one.

        if (data == "success")
        {
            $('.session_hide').hide();
            $('#modal_login').modal('hide');
            $('.session_show').show();
            $('.session_fadein').fadeIn(2000);
            $('.session_opacity').css({opacity: 1}).show();
        }
Amy Neville
  • 10,067
  • 13
  • 58
  • 94
  • 2
    So now you're saying the success callback works just fine, and that you just used `queue()` a bunch of times, and failed miserably because you forgot to run `dequeue()` to execute the queue ? – adeneo Jun 09 '13 at 20:12