Reputation: 2536
Suppose i have the following ajax call:
$.ajax({
type: 'POST',
url: 'some_url',
dataType: 'json',
data: { some: data },
success: function(data){
// Success message will be shown.
window.setTimeout(function(){location.reload()}, 2000);
},
failed : function(data){
// Error message will be shown.
window.setTimeout(function(){location.reload()}, 2000);
}
});
and on the server side i have something like this:
function delete_json(){
$post = $this->input->post();
if(!empty($post)){
// Do something crazy here and return the result as JSON.
header('Content-Type: application/json');
echo json_encode($data);
exit;
}else{
// CURRENTLY THIS DOES NOT WORK......
// IT DOES NOT REDIRECT THE PAGE AS INTENDED
redirect('some_url', 'refresh');
}
}
How can i force redirect the user to another page if if the ajax call still expecting a result to be returned?
What would be a good approach to this?
Upvotes: 0
Views: 1334
Reputation: 3790
Because it's an AJAX call it won't have a visible impact on the browser. You will have to do the redirect on the client side.
$.ajax({
type: 'POST',
url: 'some_url',
dataType: 'json',
data: { some: data },
success: function(data){
// Success message will be shown.
if(data.good===true){
window.setTimeout(function(){location.reload()}, 2000);
}
else{
window.location.href("http://my.url.here");
}
},
failed : function(data){
// Error message will be shown.
window.setTimeout(function(){location.reload()}, 2000);
}
});
Upvotes: 2