如何将数据从控制器发送回视图?
说我有以下搜索控制器:
function search()
{
$search_text = $this->input->post('company');
$data['found_companies'] = $this->Company->get_companies_by_name($search_text);
$data['page_title'] = 'Search';
$this->load->view('head', $data);
$this->load->view('pages/after_search', $data);
$this->load->view('footer');
}
我将模型中的搜索结果从模型返回到$data['found_companies']
,如何将其作为Ajax返回数据传递?
答案 0 :(得分:2)
为什么不
function search()
{
$search_text = $this->input->post('company');
$data['found_companies'] = $this->Company->get_companies_by_name($search_text);
/*
$data['page_title'] = 'Search';
$this->load->view('head', $data);
$this->load->view('pages/after_search', $data);
$this->load->view('footer');
*/ //Remove these part as it will display your view
return json_encode($data['found_companies']);
}
使用Ajax请求(即使用jQuery);
$.post('search',myData,function(result){
console.log(result); //found_companies
});
答案 1 :(得分:0)
使用CI ajax调用,您需要做的就是回显包含信息的JSON对象,而不是加载视图。页面控制器应在页面加载时加载视图。 ajax控制器应该是分开的。
关于它的视频http://geekhut.org/2009/06/how-to-codeigniter-jquery-json/