选择2错误404(未找到)

问题描述 投票:1回答:2

我的select2有一些问题,我正在关注这个例子,但它没有显示结果。我使用ajax使它更简单。

错误

404 (Not Found)

HTML

<div class="form-group">
   <label class="col-sm-4 control-label">Product Name</label>
   <div class="col-sm-6">
      <select class="productName form-control" name="productName" id="productName"></select>
   </div>
</div>

调节器

public function GetCountryName(){
    $search = $this->input->get('search');
    $query = $this->datacomplete->Get_Country($search, 'name');
    echo json_encode($query);
}

模型

class Datacomplete extends CI_Model{

    public function Get_Country($search) {
        $this->db->select('*');
        $this->db->limit(10);
        $this->db->from('auto');
        $this->db->like('name', $search);
        return $this->db->get('auto')->result_array();
    }
}

阿贾克斯

$("#productName").select2({
    ajax: {
        url: "<?php echo base_url('auto_config/GetCountryName')?>",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                search: params.term // search term
            };
        },
        processResults: function (data) {
            var results = [];

            $.each(data, function(index, item) {
                results.push({
                    id: item.id,
                    text: item.name
                });
            });
            return {
                results: results
            };
        }
    }
});

错误消息是404找不到,我不知道为什么。

php ajax codeigniter jquery-select2
2个回答
2
投票

我解决了

模型

return $this->db->get('auto')->result_array();

to

return $this->db->get()->result_array();

阿贾克斯

url: "<?php echo base_url('auto_config/GetCountryName')?>"

to


url: "<?php echo site_url('auto_config/GetCountryName')?>"

0
投票

问题在这里:你的ajax中的url: "<?php echo base_url('auto_config/GetCountryName')?>",。控制器中的方法GetCountryName()应该可以在某个路径中访问,例如/get_countries然后你可以发送ajax到它。

© www.soinside.com 2019 - 2024. All rights reserved.