我有一个关于ajax的问题,我对它很新,所以不确定最好的程序。无论如何,我已将ajax合并到我的CodeIgniter应用程序中,但我有可能有两种不同的响应,我不太确定如何在我的ajax中处理这个问题。
我可以不刷新网址而不是将结果附加到div上吗?
在我的控制器中我有表单验证,当它为false时我想刷新以显示我的错误,但如果它返回true我想显示新页面,如果这有意义吗?
视图
$.post("<?php echo base_url(); ?>part-one",
$("form").serialize(),
function(result){
// if errors show this
$("#error").html(result);
// if there are no errors how do I check the response to refresh the page to a new url?
}, "html"
);
控制器
if($this->form_validation->run() == FALSE){
$data['content'] = "part_one";
$this->load->view('template', $data);
} else {
$data['content'] = "part_two";
$this->load->view('template', $data);
}
答案 0 :(得分:1)
如果您想重新加载页面,可以这样做;
$.ajax({
url: url,
type: 'post',
data: data,
success: function(data) {
location.reload();
}
});
答案 1 :(得分:0)
如果我理解你,那么这段代码可以帮助你...
$.post("<?php echo base_url(); ?>part-one",
$("form").serialize(),
function(result){
// if errors show this
$("#error").html(result);
if(result =='false condition response')
{
window.location.reload()
}
if(result == 'True Conditon response')
{
window.location.href('URL of the page')
}
// if there are no errors how do I check the response to refresh the page to a new url?
}, "html"
);