如何使用AJAX重定向?

时间:2011-04-10 23:33:58

标签: jquery ajax

我有以下代码:

function get_login()
{
    hideshow('loading2',1);
    error(0);

    $.ajax({
        type: "POST",
        url: "The URL",
        data: $('#logins').serialize(),
        dataType: "json",
        success: function(msg){  

            if(parseInt(msg.status)==1)
            {
                succ2(1,msg.txt);                   
                setTimeout('go_to_you_page()', 1000);

            } else if(parseInt(msg.status)==0) {
                error2(1,msg.txt);
            }

            hideshow('loading2',0);
        }
    });

    return false;
}

function go_to_you_page()  
{  
    window.location = 'myaccount.php';     
} 

如果用户未登录,则会添加index.php?redirect=inbox.php(例如)

登录/inbox.php后如何重定向用户?

提前谢谢!

1 个答案:

答案 0 :(得分:4)

以下some code获取网址参数:

function get_url_parameter(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if ( results == null )
      return "";
    else
      return results[1];
}

您可以在go_to_you_page()功能中使用此功能:

function go_to_you_page()
{
    var redirect_parameter = get_url_parameter('redirect');

    // redirect to /myaccount.php if the redirect parameter is not set
    var redirect = ( redirect_parameter === "" ) ? 'myaccount.php' : redirect_parameter;

    window.location.href = redirect;
}