我试图按照这里找到教程:http://www.ifadey.com/2010/06/crud-using-jquery-and-codeigniter/我的代码进行更新,以适应对CI和JQ更新。但是当试图从我的数据库加载数据并将其显示在我的视图中时,没有任何反应/显示。指向javascript(JQ)代码的所有链接均可工作,并且所有内容均使用MAMP在本地运行。任何和所有的帮助,不胜感激。新手:笨,jQuery和阿贾克斯 - 没有返回/显示值
我的代码如下,
型号:
<?php
class mUsers extends CI_Model {
public function getAll() {
//get all records from users table
$this->db->_compile_select();
$query=$this->db->get('users')->result();
//OR $query = $this->db->get('users');
echo $this->db->last_query();
if($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
} //end getAll
} //end class
?>
控制器:
<?php
class Site extends CI_Controller{
public function index(){
$this->load->model('mUsers');
$this->load->view('home');
}
public function read(){
header('Content-Type: application/json',true);
echo json_encode($this->mUsers->getAll());
}
}
?>
查看:
<html>
<head>
<title>Test</title>
<base href="<?php echo base_url(); ?>" />
<link type="text/css" rel="stylesheet" href="css/styles.css" />
<link type="text/css" rel="stylesheet" href="css/ui-lightness/jquery-ui-1.8.12.custom.css" />
</head>
<body>
<div id="tabs">
<ul>
<li><a href="#read">Read</a></li>
<li><a href="#create">Create</a></li>
</ul>
<div id="read">
<table id="records"></table>
</div>
<div id="create">Create form goes here...</div>
</div> <!-- end tabs -->
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="js/jquery.tmpl.js"></script>
<script type="text/template" id="readTemplate">
<tr>
<td>${id}</td>
<td>${name}</td>
<td>${email}</td>
</tr>
</script>
<script type="text/javascript" src="js/all.js"></script>
</body>
</html>
尝试了您的建议,但仍然没有运气。有没有建议的方式来调试代码?谢谢! – user464180 2011-06-08 19:02:37
如果我编辑我的控制器函数以包含$ this-> load-> model('mUsers');之前的回波json_encode,然后我粘贴直接访问控制器(http://127.0.0.1/CI/index.php/site/read)的URL我最终得到的数据返回为:[{“ID”: “1”, “名”: “哈桑Fawad”, “电子邮件”: “[email protected]”},{ “ID”: “2”, “名”: “比尔·盖茨”, “电子邮件”:“法案@ test.com “},{” ID “:” 3" , “名”: “史蒂夫·乔布斯”, “电子邮件”: “[email protected]”},{ “ID”: “4”, “名” : “函数naveed艾哈迈德”, “电子邮件”: “[email protected]”},{ “ID”: “5”, “名”: “Zee的先生”, “电子邮件”: “[email protected]”}] ,但仍然无法将其加载到显示器中 – user464180 2011-06-08 19:54:10
您的ajax调用获取该数据的位置在哪里?在你的代码中没有这样的JavaScript。 – 2011-06-09 10:02:34