我花了我所有的時間來解決這個問題。返回的結果不能用typeahead和bootstrap顯示
我試圖使用bootstrap + typeahead進行函數調用ajax調用。
如果有人能幫助我,這將是偉大
這是我HTML部分:
<div class="control-group">
<label class="control-label">Parent</label>
<div class="controls">
<input type="text" value="" name="parent" id="parent" autocomplete="off" data-provide="typeahead" />
</div>
這是我JS部分:
$(document).ready(function() {
$('#parent').typeahead({
source: function (query, process) {
return $.ajax({
minLength: 1,
url: "/ajax/places/",
type: 'POST',
data : 'query='+query,
dataType: 'json',
success: function (data) {
return typeof data == 'undefined' ? false : process(data);
}
});
}
});
});
我c一看到Ajax的發射,以及的Json,這裏是摘錄:
[
"name": "Aix"
, "name": "Aix"
, "name": "Aix en Diois"
, "name": "Aix en Ergny"
, "name": "Aix en Issart"
, "name": "Aix en Othe"
, "name": "Aix en Provence"
, "name": "Aix la Fayette"
, "name": "Aix les Bains"
, "name": "Aix Noulette"
, "name": "Aixe sur Vienne"
, "name": "Artaix"
, "name": "Baix"
, "name": "Baixas"
, "name": "Benaix"
, "name": "Caix"
, "name": "Caixas"
, "name": "Caixon"
, "name": "Carhaix Plouguer"
, "name": "Chaix"
]
如果我 「的console.log(數據)」,一切似乎是確定。
感謝您的幫助!
它的工作原理,如果我刪除「名稱」屬性,像:
[
"Aix"
, "Aix"
, "Aix en Diois"
, "Aix en Ergny"
, "Aix en Issart"
, "Aix en Othe"
, "Aix en Provence"
, "Aix la Fayette"
, "Aix les Bains"
, "Aix Noulette"
, "Aixe sur Vienne"
, "Artaix"
, "Baix"
, "Baixas"
, "Benaix"
, "Caix"
, "Caixas"
, "Caixon"
, "Carhaix Plouguer"
, "Chaix"
]
但是現在我如何使用ID和名稱?
編輯:我用這個Bootstrap typeahead ajax result format - Example和發現解決方案
我會告訴你我做了什麼與其他過程。
這不能成爲你的JSON,這不是一個有效的JSON對象,也不是一個有效的JavaScript對象,甚至數組文本。你確定這是你得到的JSON嗎? –
也許他鍵入方括號而不是大括號。它可能發生。 – Shmiddty
但這並不能解釋多個「名稱」屬性。 (這可能是無效的JSON) – Shmiddty