假设我有以下ajax调用:
$.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);
}
});
在服务器端我有这样的东西:
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');
}
}
如果ajax调用仍然期望返回结果,我如何强制将用户重定向到另一个页面?
对此有什么好处?
答案 0 :(得分:2)
因为它是一个AJAX调用,所以它不会对浏览器产生明显的影响。您必须在客户端进行重定向。
$.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);
}
});