JQuery Login Redirect. Code Included

2019-07-19 17:08发布

Hey What am I doing wrong here? Extremely new to ajax.

$(document).ready(function(){
$('#submit').click(function() {
    $('#waiting').show(500);
    $('#empty').show(500);
    $('#reg').hide(0);
    $('#message').hide(0);

    $.ajax({
        type : 'POST',
        url : 'logina.php',
        dataType : 'json',
        data: {
            type : $('#typeof').val(),
            login : $('#login').val(),
            pass : $('#pass').val(),


        },
        success : function(data){
            $('#waiting').hide(500);
            $('#empty').show(500);
            $('#message').removeClass().addClass((data.error === true) ? 'error' : 'success')
                .text(data.msg).show(500)
            if(data.error === false) 
                window.location.replace("http://blahblah.com/usercp.php");      
            if (data.error === true)
                $('#reg').show(500);
                $('#empty').hide()
        },

        error : function(XMLHttpRequest, textStatus, errorThrown) {
            $('#waiting').hide(500);
            $('#message').removeClass().addClass('error')
                .text("There was an Error. Please try again.").show(500);
            $('#reg').show(500);
            $('#empty').hide();
                                Recaptcha.reload();

        }
    });

    return false;
});

});

It logs in just fine and gives the success message just fine just no luck on the window.location.replace. How can I fix this?

3条回答
男人必须洒脱
2楼-- · 2019-07-19 17:38

Try this:

...
if(data.error === 'false') 
window.location.replace("http://stackoverflow.com");
...

or

...
if(data.error == 'false') 
window.location.replace("http://stackoverflow.com");
...
查看更多
欢心
3楼-- · 2019-07-19 17:49

you missed some semicolon (will break in IE):

$('#message').removeClass().addClass((data.error === true) ? 'error' : 'success')
                .text(data.msg).show(500); //<- HERE
(...)
$('#empty').hide(); // <- HERE

edit: now you use a lot of show/hide events, you want to show a message, and then do the redirect. You should chain your redirect at the end of the show event (so the show animation wait until the end before doing the redirect:

$('#message').removeClass()
    .addClass((data.error === true) ? 'error' : 'success')
    .text(data.msg).show(500, function(){
        if(data.error === false) 
                window.location.replace("http://blahblah.com/usercp.php");      
        (...)
    });

But anyway, other responses certainly contains the main cause of your bug. i.e debug what you have in data.error, it's maybe not false.

查看更多
爷的心禁止访问
4楼-- · 2019-07-19 17:57

It looks like data.error is true, even though you ended up in the success branch. Try putting alerts to clarify your program flow.

success : function(data){ alert('success branch'); $('#waiting').hide(500); $('#empty').show(500); $('#message').removeClass().addClass((data.error === true) ? 'error' : 'success') .text(data.msg).show(500) if(data.error === false) alert('no data error'); window.location.replace("http://blahblah.com/usercp.php");
if (data.error === true) alert('data error'); $('#reg').show(500); $('#empty').hide() },

查看更多
登录 后发表回答