我的js功能是
function view_customerTOTable(){
var name = $('#name').val();
//alert(name);
$.ajax({
type: 'post',
url: 'viewCustomerData',
data: name,
success: function (data) {
alert('test'+data);
var result = JSON.parse(data);
var datas = [];
for (var x = 0; x < result.length; x++) {
datas.push('<tr id="op' + x + '">'
+ '<td hidden="true">' + result[x].C_email + '</td>'
+ '<td>' + result[x].C_clientint + '</td>'
+ '<td align="center">' + result[x].C_status + '</td>'
+ '<td align="center">' + result[x].C_clientcode + '</td>'
+ '<td align="center">' + result[x].C_name + '</td>'
+ '<td align="right">' + result[x].C_nic + '</td>'
+ '<td align="center">'
+ '<input type="button" class="btn btn-warning" value="view">'
+ '</td>'
+ '</tr>');
}
$j('#bdy_customer_details').html(datas);
//alert(data);
},
error: function(){
alert('error');
}
});
}
控制器功能
function viewCustomerData(){
$this->load->model('customer/customer_model');
echo json_encode($this->customer_model->loadCustomerDetails());
}
从此模型函数返回的值
function loadCustomerDetails(){
//$query = $this->db->select('*')->from('customer');
$sql = "SELECT * FROM customer where C_name = '{$_POST['name']}'";
//$send = $query->mod_select();
try {
return $this->db->select($sql);
} catch (Exception $exc) {
return $exc->getTraceAsString();
}
}
查看页面是
<table id="customer_details" class="table table-bordered" cellspacing="1" cellpadding="1" width="100%" >
<thead>
<tr>
<td><font type="curlz MT Regular" color="black">Asset Type</font></td>
<td><font type="curlz MT Regular" color="black">Client Code</font></td>
<td><font type="curlz MT Regular" color="black">Contract No</font></td>
<td><font type="curlz MT Regular" color="black">Contract Name</font></td>
<td><font type="curlz MT Regular" color="black">Customer Name</font></td>
<td><font type="curlz MT Regular" color="black">NIC No.</font></td>
<td><font type="curlz MT Regular" color="black">View</font></td>
<!--<td>Remove</td>-->
</tr>
<thead>
<tbody id="bdy_customer_details" >
</tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td id="tdet1"></td>
<td id="tdet2"></td>
<td id="tdet3"></td>
</tr>
</tfoot>
</table>
我想在表格中显示这些数据。但我无法返回数据。在js函数中移动到成功点。但是json.parse指出它不起作用
答案 0 :(得分:0)
在您的控制器操作更改下面的代码。在您的代码中,您不是从模型返回数据。它只返回测试。
die(json_encode(array('result' => 'test')));
//as per updated question
die(json_encode($this->customer_model->loadCustomerDetails()));
删除此标题
header('Content-Type: application/json');
更新了javascript代码
var result = JSON.parse(data);
var datas = '';
for (var x = 0; x < result.length; x++) {
datas = '<tr id="op' + x + '">'
+ '<td hidden="true">' + result[x].C_email + '</td>'
+ '<td>' + result[x].C_clientint + '</td>'
+ '<td align="center">' + result[x].C_status + '</td>'
+ '<td align="center">' + result[x].C_clientcode + '</td>'
+ '<td align="center">' + result[x].C_name + '</td>'
+ '<td align="right">' + result[x].C_nic + '</td>'
+ '<td align="center">'
+ '<input type="button" class="btn btn-warning" value="view">'
+ '</td>'
+ '</tr>';
// if((result.length - 1) === x){
$('#bdy_customer_details').append(datas);
//}
}