开发者

Login from fancybox: reload page ONLY if login is successful?

开发者 https://www.devze.com 2023-03-18 00:41 出处:网络
I have a login form inside a fancybox that does its thing via ajax. I need that when the login is successful the page will reload, but not if the login fails. I used to have

I have a login form inside a fancybox that does its thing via ajax. I need that when the login is successful the page will reload, but not if the login fails. I used to have

'onClosed': function() {   parent.location.reload(true); }

on my fancybox but that would reload the thing even if the user just manually closed the fancybox without logging in.

I have this function for loging:

$("#loginform").bind("submit", function() {
        if ($("#username").val().length < 1 || $("#password").val().length < 1) {
            $("#login_error").show();
            $.fancybox.resize();
            return false;
        }

        $.fancybox.showActivity();

        $.ajax({
            type        : "POST",
            cache       : false,
            url         : "/login/ajax-login.php",
            data        : $(this).serializeArray(),
            success: function(data) {

                switch(data){
                    case "1"://exito
                        function() {window.location.reload(true);}
                        //THIS DOESN'T WORK
                    break;
                    case "0"://error
                开发者_开发技巧        $.fancybox("ERROR. El usuario no existe o es incorrecto.");
                    break;
                }
            }
        });

        return false;
    });                       

As you can see, when data equals 1 the login is succesful but nothing will happen. If I manually reload the page I see the login was successful tho.


A switch statement does not accept callbacks. Your code appears as if you thought they did.

You need to run that function...

function() {window.location.reload(true);}

...at the moment you have just defined an anonymous function which has not been assigned to any variable.

Easiest way is to drop the wrapper function...

window.location.reload(true);

...or if you insist of having a function (why?) you could use...

(function() {window.location.reload(true);})();

Most people use the former for information hiding, but you declare no new variables, so it'd be pointless.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号