[英]Force redirect page on $.ajax call
假设我有以下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调用仍然期望返回结果,我该如何强制将用户重定向到另一个页面?
有什么好的方法呢?
由于它是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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.