Jquery高效的开发速度,良好的封装和简洁而灵活的语法都给我留下了很深刻 的印象。 而它的ajax类让开发ajax成为了一种乐趣。
你可以ajax类轻易的POST或者GET request。 它能帮助用户完成codeigniter不能完成的功能。
CI 的MVC 分离也做的非常的好,令人眼前一亮。但不可否认,ci也有它存在的问题。那就是对新技术的支持不够。类似json, ajax这些我们在网络开发中已经十分普及的技术,它都没有完成支持。 前些时间,由于自己想写一个php的项目,前段时间我调查了一下, 似乎大多数人都在使用xajax和其它封装好的php类。对此,我不是很喜欢,因为我一直认为框架会降低程序的执行效率。 所以我尝试着在ci框架中直接使用js框架jquery。
前天经过反复的试验,我找到了一个我自认为满意的方法, 并且发现在ci中实现ajax远比自己想像的简单。而且也没有像我自己想像的那样,破坏了MVC的完整性。
下面我把我的代码贴出来,和大家分享一下。
控制器:
function get_Photo()
{
//query the picture from model
$query = $this->photomodel->get_Pic($this->input->post('id')); // 对应jquery里面的 "id=" + $(this).attr("id"),
if($query->num_rows() > 0) {
$row = $query->row();
$output = "photo_infomation";
}else{
$output = "no picture was found";
}
$this->output->append_output($output);
// 不要用echo,因为容易跟session或者header产生冲突,还有一个方法是,如果不想用内部api,就新建个view文件,里面就一个行<?php echo$output;?>
}
视图:
$(".show_photo").click(function(){ //post the pictureID to controller and return the picture and embed it to the photoboard. $.ajax({ type:"post", data: "id=" + $(this).attr("id"), url:"<?=site_url('photogallery/getPhoto')?>", success: function(data){ alert(data); }, error: function() { alert("ajax error"); } }); });
ok