【问题标题】:Not able to display data through ajax in codeigniter无法在codeigniter中通过ajax显示数据
【发布时间】:2015-10-03 04:25:25
【问题描述】:

我想在从下拉列表中选择区域后显示餐厅。但我的代码没有显示餐厅名称和该餐厅的菜单按钮,请告诉我我做错了什么

这是我的型号代码

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>

当我发出警报时(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>

请告诉我哪里做错了

【问题讨论】:

  • alert(response.data) 并粘贴它的警报内容..

标签: php jquery arrays ajax codeigniter


【解决方案1】:

您没有正确放置来自 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",

【讨论】:

  • 我应用了你所有的 saggession 但作为响应显示相同的输出
  • 将你的控制器回显更改为echo $html;@falakhamid
  • 请告诉我我在控制器 echo 中做了哪些更改
  • echo json_encode(array('data' =&gt; $html)); 更改为 echo $html; @falakhamid
【解决方案2】:

试试下面,

你应该返回它而不是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);
        }
    });
}

【讨论】:

  • @jloker 请告诉我如何将请求标头设置为 json
  • @falakhamid 检查我上面的代码..然后用我的替换你的
猜你喜欢
  • 1970-01-01
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多