无法使用Typeahead.js预取本地json文件

时间:2018-04-09 16:56:10

标签: javascript typeahead.js

我需要使用读取本地json文件的自动完成,我正在使用Typeahead.js。

我有这个jsfiddle example,它正在获取外部json文件。我尝试以下列方式调整此代码并使用faild:

$(document).ready(function () {
    var dataSource = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('country'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch: "../app/helpers/countries.json"
    });


    dataSource.initialize();

    $('.typeahead').typeahead({
        highlight: true
    }, {
        displayKey: 'country',
        source: dataSource.ttAdapter()
    });
});

countries.json

[
  {
        "country": "Holland",
        "city": "Amsterdam"
    },
    {
        "country": "Belgium",
        "city": "Brussel"
    },
    {
        "country": "Germany",
        "city": "Berlin"
    },
    {
        "country": "France",
        "city": "Paris"
    }
]

浏览器上的错误消息:

  

client.js:995 GET http://localhost:3000/app/helpers/countries.json 406   (不可接受)

根据这个typeahead examples它应该有效,我不知道为什么。

0 个答案:

没有答案
相关问题