-4

我正在使用这样的 Jquery ajax:

function deactivate() {
    var user_info=document.getElementById('user_info').value;
    if (user_info=''){
    alert('Enter password!');
    return false;
    }
    else {
      $.ajax({
                type: "GET",
                url: "/deactivate/",
                data: {user_info:user_info},
                success: function(data){
                     $('#deactivate_modal').html(data)

                }
            });
        }
}

如果用户提供的用户信息不正确,则错误会显示在 div 中,这很好,但如果正确,我希望刷新页面,将用户重定向到登录页面。我怎样才能做到这一点?

4

1 回答 1

1

如果你的服务器返回一个响应(可以是任何东西,html、json、纯文本等),那么你可以在你的成功函数中检查它。如果用户信息正确,下面的代码期望服务器返回“成功”响应。响应可以是任何东西,只需将“成功”替换为服务器的响应即可。

我还注意到您在第一个 if 语句中使用了赋值运算符,并且您的键/值对需要将键括在引号中,否则它将替换为变量的内容。

代码示例:

function deactivate() {
    var user_info = document.getElementById('user_info').value;
    if (user_info == '') {
        alert('Enter password!');

        return false;
    }
    else {
        $.ajax({
            type:    "GET",
            url:     "/deactivate/",
            data:    {'user_info': user_info},
            success: function (data) {
                if (data === 'success') {
                    window.location = 'http://www.mydomain.com/myPageAFterLogin'
                } else {
                    $('#deactivate_modal').html(data)
                }
            }
        });
    }
}
于 2013-04-25T17:24:45.460 回答