Typeahead.js无法在远程工作

时间:2015-11-10 21:20:29

标签: javascript json typeahead.js bloodhound

以下代码完美无缺。但是,将JSON保存为单独的文件时,它将无法再工作。

var customersData = [
    {
        "id": "1",
        "name": "Stuart Smith"
    },
    {
        "id": "2",
        "name": "James Leward"
    },
    {
        "id": "3",
        "name": "Steve Mitchell"
    },
    {
        "id": "4",
        "name": "George Clifford"
    },
    {
        "id": "5",
        "name": "Emma Clark"
    }
 ]

var customers = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: customersData
    });

    customers.initialize();

    $('.typeahead').typeahead(
        null, {
        name: 'customers',
        displayKey: 'name',
        source: customers.ttAdapter()
    }).on('typeahead:selected', function(event, data){            
        console.log(data.id);   
    });

我认为我只需要将local: customersData更改为remote: { url: "data.json" }但不会成功。我错过了什么吗?

0 个答案:

没有答案