Jquery 如何使用AJAX重定向?
我有以下代码:Jquery 如何使用AJAX重定向?,jquery,ajax,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(p
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
后如何重定向用户
提前谢谢你 下面是获取URL参数的步骤:
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\u to\u You\u页面()中使用此选项。
函数:
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;
}
问题到底是什么?您似乎拥有基于链接实现重定向的正确代码。请在成功处理程序中设置window.location,或在XHR回复中使用响应重定向进行回复。回答正确,但根据帖子window.location.replace比window.location.href更好。请查看