Bootstrap Select2 - Unable to populate via AJAX

2019-08-09 08:55发布

问题:

I need some help figuring out what I am doing incorrectly. I am trying to populate the dropdown with users from my database. I am using Codeigniter and Firebug is giving me the error:

TypeError: j is undefined

VIEW

 <input id="users" type="hidden">

 <script>
 $("#users").select2({
        width: "element",
        ajax: {
            url: "localhost/index.php/get_clients",
            dataType: 'json',
            data: function (term, page) {
                return {
                    q: term
                };
            },
            results: function (data, page) {
                return { results: data };
            }
        }
  });
  </script>

CONTROLLER

function get_clients() {
    $this->load->model('users_model');
    $result = $this->users_model->get_all_clients();
 }

MODEL

 function get_all_clients() {
    $all_clients = $this->db->select('CONCAT(first_name, " ", last_name) as text, id', FALSE)
    ->get('clients')->result();
    $rows = array();
        foreach ($all_clients as $entry) {
            $rows[] = $entry;
        }
        print json_encode($rows);
}

Which returns something like this:

  [{"text":"John Smith","id":"433"},{"text":"Paul Sparks","id":"434"}]

回答1:

Sorry, I was being stupid. I figured it out. User error.