无法通过codeigniter中的ajax显示数据

时间:2015-10-03 04:25:26

标签: php jquery arrays ajax codeigniter

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

我的模型代码

    public class BillDetailsPanel implements ActionListener {


        JPanel panel;
        int flag = 0;
        JLabel lItemName, lPrice, lQty, ltax, lDisPrice;
        JTextField price, qty, tax, disPrice;
        JComboBox<String> itemName;
        String[] bookTitles = new String[] { "Effective Java", "Head First Java",
                "Thinking in Java", "Java for Dummies" };

        JButton addBtn

        public BillDetailsPanel() {
            panel = new JPanel();
            panel.setPreferredSize(new Dimension(900, 50));
            FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 5, 15);
            panel.setLayout(layout);
            // panel.setBackground(Color.GREEN);

            lItemName = new JLabel("Item Name");
            lPrice = new JLabel("Price");
            lQty = new JLabel("Quantity");
            ltax = new JLabel("Tax");
            lDisPrice = new JLabel("Discount Price");

            itemName = new JComboBox<String>(bookTitles);
            itemName.addActionListener(this);
            price = new JTextField(8);
            // price.setEditable(false);
            qty = new JTextField(4);
            tax = new JTextField(5);
            // tax.setEditable(false);
            disPrice = new JTextField(8);

            addBtn = new JButton("Add");
            addBtn.addActionListener(this);

            panel.add(lItemName);
            panel.add(itemName);
            panel.add(lPrice);
            panel.add(price);
            panel.add(lQty);
            panel.add(qty);
            panel.add(ltax);
            panel.add(tax);
            panel.add(lDisPrice);
            panel.add(disPrice);
            panel.add(addBtn);
            panel.setVisible(true);

        }
     public void actionPerformed(ActionEvent e) {

    BillTablePanel btp=new BillTablePanel();
            String[] data=new String[5];


            data[0]=(String) itemName.getSelectedItem();
data[1]=price.getText();
    data[2]=qty.getText();
    data[3]=tax.getText();
    data[4]=qty.getText();
    btp.model.addRow(data);
           btp.model.addRow(data);
            System.out.println(data+"dataaaaaaaaaaaa");     

        }

    }



    }


    public class BillTablePanel implements ActionListener{
        public JPanel panel;
        public JTable table;
        public JScrollPane scrollPane, scrollPane1;
        public DefaultTableModel model;
        public int a=10;
    String[] data=new String[5];
        public BillTablePanel () {

            panel = new JPanel();
            panel.setLayout(null);

            model = new DefaultTableModel();

            String columnNames[] = { "Item Name", "Actual Price", "Qty", "Tax",
                    "Price" };

            table = new JTable();
            model.setColumnIdentifiers(columnNames);
            table.setModel(model);
            table.setFocusable(false);
            scrollPane = new JScrollPane(table);
            scrollPane.setBounds(0, 0, 850, 100);

            panel.add(scrollPane);
        }

    <br>


    public class TestClassFrame {
        JFrame f;

        BillDetailsPanel bill = new BillDetailsPanel();
        BillTablePanel billTablePanel = new BillTablePanel();
        public TestClassFrame() {

            f = new JFrame("Zeon Systems");
            f.setLayout(null);

            bill.panel.setBounds(0, 0, 900, 100);

            f.add(bill.panel);
            billTablePanel.panel.setBounds(0, 100, 900, 500);
            f.add(billTablePanel.panel);
            f.pack();
            f.setSize(900, 550);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

         public static void main(String[] args) {
         new TestClassFrame();
         }

}

控制器代码

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);
        }
    });
}

当我做警报时(response.data);

<div id="restaurant">


</div>

请告诉我我错在哪里

2 个答案:

答案 0 :(得分:0)

您没有正确地从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",

答案 1 :(得分:0)

尝试下面的内容,

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