我試圖預取整JSON數據庫(55KB),使用它與typeahead.js 0.11.1。我在這一天掙扎了整整一天,我發現typeahead.js文檔非常基本。Twitter的typeahead.js 0.11.1預取不工作
我的JSON看起來是這樣的:
[{
"id": 1,
"name": "Green"
}, {
"id": 2,
"name": "Red"
}, {
"id": 3,
"name": "Blue"
}]
和JavaScript:
$(function() {
var tagSuggestion = new Bloodhound({
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.name);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
url: 'ajax.json-colors.php'
}
});
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 2
}, {
name: 'tagSuggestion',
displayKey: 'name',
source: tagSuggestion.ttAdapter()
});
});
我不知道我做錯了什麼,而是提前鍵入不帶預取工作。
您可能需要重命名您的JSON文件'ajax.json-colors.json',而不是'ajax.json-colors.php',並確保路徑存在。檢查是否有任何控制檯錯誤。 –
@ArathiSreekumar數據庫應該是動態的,是沒有問題的接收數據到JavaScript對象 – denoise
所以你可以看到未來通過給前端的數據?預取確實會發出ajax請求? –