【发布时间】:2016-01-28 14:59:11
【问题描述】:
我正在尝试从 json 中返回特定数据,但它什么也没返回。
我以http://jsfiddle.net/Fresh/1hrk0qso/ 为例 使用示例 url 时效果很好。
url: 'https://cdn.rawgit.com/twitter/typeahead.js/gh-pages/data/countries.json',
当我从 localhost 执行此操作时它也有效
url: '../json/countries.json',
但是,当我更改 country.json 文件以包含多个字段(请参阅下面的 JSON)时,它不会返回任何内容。
JSON:
[
{"cities_id":"1","city":"Attignat","postal_code":"01340"},
{"cities_id":"2","city":"Beaupont","postal_code":"01270"},
{"cities_id":"3","city":"B\u00e9ny","postal_code":"01370"}
]
JS(仅修改为返回 'city' 以替换 JSON 文件中的 'name'):
var countries = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('city'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: '../json/json_cities.json',
filter: function (countries) {
return $.map(countries, function (city) {
return {
city: city
};
});
}
}
});
// Initialize the Bloodhound suggestion engine
countries.initialize();
$('.typeahead_city').typeahead(null, {
name: 'city',
displayKey: 'city',
source: countries.ttAdapter()
});
我真的不明白,因为它也与示例大致相同: How do I list objects for Typeahead.js and/or with the Bloodhound engine?
非常感谢
【问题讨论】:
标签: json object twitter-bootstrap-3 bootstrap-typeahead