2016-11-11 108 views
0

任何想法如何調試select2的ajax功能?下面是我想要實現一個例子:https://jsbin.com/sohupoviko/edit?html,output和 這裏的結果爲http://localhost:4000/users設置:嘗試使用ajax與select2時出現錯誤

[ 
    { 
    "id": 1, 
    "first_name": "John", 
    "last_name": "Smith" 
    }, 
    { 
    "id": 2, 
    "first_name": "John2", 
    "last_name": "Smith2" 
    } 
] 

這是當我在本地運行的代碼返回本地的錯誤:

jquery.min.js:2 jQuery.Deferred exception: Cannot read property 'length' of undefined TypeError: Cannot read property 'length' of undefined 
    at d (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js:2:5214) 
    at Object.f.transport.d.status.0.d.status.e.trigger.message (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js:2:4227) 
    at j (http://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:29948) 
    at k (http://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:30262) undefined 

任何想法的根源可能是什麼或如何調試?

回答

0

在你的後端代碼請修改查詢中使用「文本」

例子:

$query = $mysqli->query("SELECT id, first_name, last_name AS text FROM users WHERE fist_name LIKE '%$term%'")); 
while ($row = mysql_fetch_assoc($query)) { 
     $return[] = $row; 
    } 

echo json_encode($return); 

幸得 - >Select2.js error: Cannot read property 'length' of undefined

+0

我的後端是不同的。這是一個簡單的db.json文件,帶有一些硬編碼的json對象。 json-server將文件信息返回爲json。我確信所有的屬性名稱和值都包含在引號*中,但這並沒有解決問題*。是否有一些類型的附加例程可以對結果對象執行,如JSON.stringify()或分解select2處理函數以解決此錯誤?小號 – user7066345

相關問題