Php Codeigniter中的AJAX和JSON
我有一个网站,我想通过AJAX和JSON获得一些查询结果,但我完全不知道怎么做,我有这个功能Php Codeigniter中的AJAX和JSON,php,jquery,ajax,json,codeigniter,Php,Jquery,Ajax,Json,Codeigniter,我有一个网站,我想通过AJAX和JSON获得一些查询结果,但我完全不知道怎么做,我有这个功能 public function category() { $table = $this->uri->segment(2); $content_id = $this->uri->segment(3); $data['content'] = $this->site_model->get_content($table, $co
public function category() {
$table = $this->uri->segment(2);
$content_id = $this->uri->segment(3);
$data['content'] = $this->site_model->get_content($table, $content_id);
$this->load->view('template/right-content', $data);
}
本质上,运行的查询是动态的,这取决于传递的url,我需要的是,用户单击类似的链接
<a href="/category/blog/1" id="blog">Read the blog</a>
从这个链接中,我得到blog和1,并将其传递到查询中,但我需要将结果加载到一个视图中,该视图加载到我的主模板中,然后每次单击链接时,在不覆盖以前的数据的情况下执行相同的操作,有人知道如何做到这一点吗 如果我理解正确,您需要向该url发送Ajax请求,然后将其附加到文档中的适当位置。大概是这样的:
$("#blog").click(function () {
var url = $(this).attr("href");
$.ajax ({
url: url,
type: "POST",
success : function (html) {
$("#someDiv").append(html);
}
});
});
因此Codeigniter视图应该只包含内容,实际上,可能还包含一些必要的标记来设置样式。内容所在的容器应该已经位于链接所在的页面中 或者,如果您真的希望数据以JSON的形式返回,您可以这样做
public function category() {
$table = $this->uri->segment(2);
$content_id = $this->uri->segment(3);
echo json_encode($this->site_model->get_content($table, $content_id));
}
而且,如果您使用上面的authors方法使用$.append,您可能希望修改控制器:
public function category() {
$table = $this->uri->segment(2);
$content_id = $this->uri->segment(3);
$data['content'] = $this->site_model->get_content($table, $content_id);
echo $this->load->view('template/right-content', $data, TRUE);
}