[英]Not able to display data through ajax in codeigniter
我想在从下拉列表中选择其区域后显示该餐厅。 但我的代码没有显示餐厅名称和那家餐厅的菜单按钮请告诉我我在哪里做错了
它是我的模型代码
function select_record($table, $where = NULL)
{
$this->db->select();
if ($where) $this->db->where($where);
$this->db->from($table);
$query = $this->db->get();
// echo $this->db->last_query();
return $query->result();
}
控制器代码
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array(
'city_id' => $cit_id,
'city_area_id' => $area
);
$data = $this->bulk->select_record('restaurant', $where);
$html = '<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th> No. </th>
<th style=""> Restaurant Names </th>
</tr>
</thread>
<tbody>
<th> <span value="'. $data[0]->restaurant_id . '" class="res_id"></span></th>
<th style=""> </th>
<th style=""> <span value="'. $data[0]->restaurant_name . '" class="res_id"></span> </th>
<th style="width: 1%" > <a href="<?php echo base_url(); ?>index.php/BulkRecipe_Controller/bulk_recipe/<?php echo $row->restaurant_id; ?>" class="btn btn-warning" <i class="glyphicon-edit"></i>See Menu</a> </th>
</tr>
</tbody>
</table>
</div>';
echo json_encode(array(
'data' => $html
));
}
脚本代码
function get_rests() {
var city_id = $('#city_id').val();
var area_id = $("#area_id").val();
$.ajax({
type: "POST",
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
data: {
cit_id: city_id,
areaID: area_id
},
dataType: 'json',
cache: false,
success: function (response) {
alert(response);
$('#restaurant').html(response.data);
}
});
}
它的视图代码
<div id="restaurant">
</div>
当我做 alert(response.data);
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<th>
<span value="1" class="res_id"></span></th>
<th style="">
</th>
<th style="">
<span value="salten paper" class="res_id"></span>
</th>
<th style="width: 1%" >
<a href="<?php echo base_url();?>index.php/BulkRecipe_Controller/bulk_recipe/<?php echo $row->restaurant_id;?>" class="btn btn-warning" <i class="glyphicon-edit"></i>See Menu</a>
</th>
</tr>
</tbody>
</table>
</div>
请告诉我我哪里做错了
您没有正确放置来自 ajax 的response
。 $('#restaurant').html(response.data);
应该:
$('#restaurant').html(response);
也变
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
到
url: "<?=base_url();?>index.php/bulk_controller/get_rests",
试试下面的,
您应该返回它而不是echo
,您还必须将请求标头设置为json
模型:
function select_record($table, $where = NULL)
{
$this->db->select('*');
$this->db->from($table);// this should come right after select statement
if ($where) $this->db->where($where);
$query = $this->db->get();
// echo $this->db->last_query();
return $query->result();
}
控制器:
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array(
'city_id' => $cit_id,
'city_area_id' => $area
);
$data = $this->bulk->select_record('restaurant', $where);
$html = '<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th> No. </th>
<th style=""> Restaurant Names </th>
</tr>
</thread>
<tbody>
<th> <span value="'. $data[0]->restaurant_id . '" class="res_id"></span></th>
<th style=""> </th>
<th style=""> <span value="'. $data[0]->restaurant_name . '" class="res_id"></span> </th>
<th style="width: 1%" > <a href="<?php echo base_url(); ?>index.php/BulkRecipe_Controller/bulk_recipe/<?php echo $row->restaurant_id; ?>" class="btn btn-warning" <i class="glyphicon-edit"></i>See Menu</a> </th>
</tr>
</tbody>
</table>
</div>';
return $this->output->set_content_type('application/json')->set_output(json_encode(array(
'data' => $html,
)));
}
脚本:
function get_rests() {
var city_id = $('#city_id').val();
var area_id = $("#area_id").val();
$.ajax({
type: "POST",
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
data: {
'cit_id': city_id,
'areaID': area_id
},
dataType: 'json',
cache: false,
success: function (response) {
alert(response);
console.log(response)
$('#restaurant').html(response.data);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.