Select2.js error: Cannot read property 'length' of undefined

前端 未结 3 1160
不思量自难忘°
不思量自难忘° 2021-01-13 04:01

I am using Select2 jquery plugin and I can\'t get results with json. When looking json response in browser it looks ok. Like this for example:

[{
               


        
3条回答
  •  心在旅途
    2021-01-13 04:48

    Note: just a stab at it. Just what stuck out.

    Your json has no property results, so try.

    $("#select2_family").select2({
      minimumInputLength: 3,
      ajax: {
       url: "json_family.php",
       dataType: 'json',
       data: function (term) {
           return {
             term: term,
           };
       },
       results: function (data) {
    
         // CHANGED
         return { results: data };
    
       }
    
      }
    });
    

    changed the query -- see if this helps

    $myArray = array();
    
    // here
    if ($result = $mysqli->query("SELECT id, family AS text FROM family WHERE family LIKE '%$term%'")) {
        $tempArray = array();
        while($row = $result->fetch_object()) {
                $tempArray = $row;
                array_push($myArray, $tempArray);
            }
        echo json_encode($myArray);
    }
    

提交回复
热议问题