Imam Nur
Imam Nur

Reputation: 61

Select2 error 404 (Not Found)

I have some issues with my select2, I'm following the example but it doesn't show the result. I use ajax to make it more simple.

error

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>

controller

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

model

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

ajax

$("#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
            };
        }
    }
});

the error messages is 404 not found, I don't know why.

Upvotes: 1

Views: 983

Answers (2)

Imam Nur
Imam Nur

Reputation: 61

I solved it.

model

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

to

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

ajax

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

to


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

Upvotes: 2

bigwolk
bigwolk

Reputation: 418

Problem is here: url: "<?php echo base_url('auto_config/GetCountryName')?>", in your ajax. Method GetCountryName() in your controller should be accesible in some path, for example /get_countries then You can send ajax to it.

Upvotes: 0

Related Questions