How to redirect using AJAX?
I have the following code:
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)
{
开发者_JS百科 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';
}
If the user is not logged in, it will add index.php?redirect=inbox.php
(for example)
How can i redirect the user after logging in to the /inbox.php
?
Thank you in advance!
Here's some code to get a URL parameter:
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];
}
You can use this in your go_to_you_page()
function:
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;
}
精彩评论