成功提交Ajax后刷新无效

时间:2013-01-10 09:27:43

标签: jquery

$(document).ready(function(){

    $("#login").click(function(){
    $.post($('#loginform').attr("action"), $('#loginform').serializeArray(), function(data) {
                if(data == 'Success'){
                    $(document).ajaxStop(function() { location.reload(true); });
                }else {
                    $('#loginmsg').html(data);
                }
        });
    });
});

除非在成功提交后页面没有刷新,否则一切都工作正常。怎么办呢?

服务器端:

<?php
if (empty($_POST) === false){
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) === true || empty($password) === true){
        $errors[] = 'You need to enter a username or password';
    }
    else if (user_exists($username)===false){
        $errors[] = 'We can\'t find that username in our database.';
    }
    else if (user_active($username) === false){
        $errors[] = 'Activate your account.';
    }   else    {
        $login = login($username,$password);
        if ($login === false){
            $errors[] = 'Incorrect combination.';
        } else {
            $_SESSION['user_id'] = $login;
            $errors[] = 'Success';
            exit();
        }
    }
}else {
    $errors[] = 'No data received!';
}
output_errors($errors);
?>

3 个答案:

答案 0 :(得分:0)

你需要写:

window.location.reload()

OR

window.location.href=window.location.href

答案 1 :(得分:0)

您无需致电ajaxStop()。将您的成功代码更改为:

if(data == 'Success'){
  location.reload(true);
}

答案 2 :(得分:0)

删除PHP中对exit()的调用。它阻止它在Success案例中转到output_errors($errors),因此您永远不会将Success消息发送给客户端。