2
這是我的index.php觀點是我的表被放置如何顯示我的表格?
<table class="striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Address</th>
<th>Created at</th>
</tr>
</thead>
<tbody id="showdata">
<!--<tr>
<td>1</td>
<td>Alvin</td>
<td>Eclair</td>
<td>$0.87</td>
<td>
<a href="javascript:;" class="waves-effect waves-light btn">Edit</a>
<a href="javascript:;" class="waves-effect waves-light btn">Delete</a>
</td>
</tr>-->
</tbody>
</table>
這是我的AJAX腳本放在index.php文件
function showAllEmployee() {
$.ajax({
type: 'ajax',
url: '<?php echo base_url(); ?>index.php/Employee/showAllEmployee',
async: false,
dataType: 'json',
success: function(data) {
//console.log(data);
var html = '';
var i;
for (i=0; i<data.length; i++) {
html += '<tr>'+
'<td>'+data[i].emp_id+'</td>'+
'<td>'+data[i].name+'</td>'+
'<td>'+data[i].address+'</td>'+
'<td>'+data[i].created_at+'</td>'+
'<td>'+
'<a href="javascript:;" class="waves-effect waves-light btn" style="margin-right: 10px;" data="'+data[i].emp_id+'">Edit</a>'+
'<a href="javascript:;" class="waves-effect waves-light btn" data="'+data[i].emp_id+'">Delete</a>'+
'</td>'+
'</tr>';
}
$('#showdata').html(html);
},
error: function() {
alert('Could not get data from database');
}
});
}
這是我有我的員工控制器上
public function showAllEmployee()
{
$result = $this->em->showAllEmployee();
echo json_encode($result);
}
這是我的模型
public function showAllEmployee()
{
$this->db->order_by('created_at', 'desc');
$query = $this->db->get('tbl_employees');
if($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
每當我刷新頁面中的數據不會顯示,而不是我碰到一個錯誤無法從它是我在我的AJAX腳本設置可能是錯誤請幫助
如果AJAX腳本打那麼控制器腳本就可以了。確保它擊中控制器 –
@masum billah我確信這個URL我可以查看我的JSON數據 – Lestah
如何調用JS函數'showAllEmployee()'?並使用'type:'get','而不是使用'ajax'。 –