使用AJAX和Codeigniter获取数据?
我试图将数据提取到表中,没有任何反应。
- 没有表格出现
- 没有提取数据
调节器
public function indexajax() { if($this->input->post("action")=='FetchAllUserUingAjax') { $this->load->model("usersmodel"); $data["allu"]=$this->usersmodel->ShowAllUsers("users"); $data['pagetitle']=" -->All Users Using Ajax<--"; foreach ($allu as $a): echo' '.$a->id.' '.$a->username.' '; endforeach; $this->load->view("template/admin/header",$data); $this->load->view("users/allusersusingajax",$data); $this->load->view("template/admin/footer"); } }
jQuery的
$(document).ready(function () { FetchAllUserUingAjax(); function FetchAllUserUingAjax() { $.ajax({ url:'Users/indexajax', method:"post", success:function (data) { $(".userdataajax table").append(data); } }) var action="FetchAllUserUingAjax"; $.ajax({ url:"Users/indexajax", method:"post", data:{action:action}, success:function (data) { $(".userdataajax table tr ").not("table tr:first").remove(); $(".userdataajax table").append(data); Table(); } }) } })
模型
public function ShowAllUsers() { $sql=$this->db->get("users"); return $sql->result(); }
视图
# name image full name email usertype status reg date reg time delete edit Activate disactivate
您的代码提示未显示的其他相关代码。 我正在把你所展示的东西当作所有需要知道的东西。 这是我基于这个前提所看到的。
一,观点。 在表中添加id
。 它使JQuery选择器变得如此简单。 JavaScript位于此文件中,即“users / allusersusingajax.php”。
# name image full name email usertype status reg date reg time delete edit Activate disactivate
控制器需要两种方法。 一个显示另一个表来获取行。 这是Users.php文件
//show the page which includes the basic and header row public function indexajax() { // The code and question text give no reason for this conditional test // So I'm removing it //if($this->input->post("action") == 'FetchAllUserUingAjax') //{ $data['pagetitle'] = "-->All Users Using Ajax<--"; $this->load->view("template/admin/header", $data); $this->load->view("users/allusersusingajax"); $this->load->view("template/admin/footer"); //} } //respond to ajax request public function get_all_users() { $this->load->model("usersmodel"); $allu = $this->usersmodel->ShowAllUsers("users"); $out = ''; //if the model returned an empty array we still have a string to echo //using PHP's output buffer to simplify creating a big string of html ob_start(); //start output buffering foreach($allu as $a): ?> = $a->id; ?> = $a->username; ?>
阅读PHP的输出控制function
我首先更新我的模型以返回一个数组:
return $sql->result_array();
然后在您的控制器中,您不需要加载视图:
public function indexajax() { if($this->input->post("action")=='FetchAllUserUingAjax') { //set content type header("Content-type: application/json"); $this->load->model("usersmodel"); echo json_encode( $this->usersmodel->ShowAllUsers(); //this method doesn't expect an argument, no need to pass one ); } }
然后在你的ajax回调中:
success: function(resp){ $.each(resp, function(k,v){ console.log(v); }); }
- 在PHP中获取无序列表的所有列表项
- Ajax Loader和链式选择框
- 使用.ajax(JQuery)跨域问题到我拥有的域 – 简单的PHP页面
- Twitter Bootstrap Popovers不适用于动态生成的内容
- 在IE和Chrome中没有显示加载的gif图像
- JavaScript中的全局变量在使用getJSON时未设置?
- 使用jQuery Ajax解析PHP对象数组
- 将json字符串作为参数传递给webmethod
- jQuery Mobile通过ajax动态创建可折叠集不适用样式
Interesting Posts
jQuery AJAX – 意外的令牌+ parsererror
Jquery AJAX发布到PHP
ajax回调不支持.done?
当数据包含连续的问号时,难以理解的jQuery $ .ajax()行为
jQuery模拟单击选项卡执行代码
使用contentType调用Ajax:’application / json’无效
IE不会触发jQuery Ajax的成功
在通过ajax提交时如何获取nicEdit表单的内容?
在使用AJAX获取html内容后,将侦听器添加到新元素的正确方法是什么? (jQuery,Javascript)
没有指定dataType的自定义ajaxTransport函数没有触发(全部!)