Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typeahead将结果显示为未定义_Javascript_Typeahead_Bootstrap Typeahead - Fatal编程技术网

Javascript Typeahead将结果显示为未定义

Javascript Typeahead将结果显示为未定义,javascript,typeahead,bootstrap-typeahead,Javascript,Typeahead,Bootstrap Typeahead,我正在尝试使用typeahead来显示谷歌的建议 Ajax调用工作正常,数据返回正确: 执行返回过程(数据)前 数据包含以“w”开头的字符串数组 数据=[“沃尔玛”、“天气”、“富国银行”、“世界星巴克”, “沃尔格林”、“维基百科”、“白页”、“世界杯”、“webmd”, “天气雷达”] 然而,显示的建议显示“未定义”而不是真实的单词。 你知道我在这里遗漏了什么吗?谢谢 <input type="text" class="typeahead" placeholder="Searc

我正在尝试使用typeahead来显示谷歌的建议

Ajax调用工作正常,数据返回正确:

执行返回过程(数据)前 数据包含以“w”开头的字符串数组

数据=[“沃尔玛”、“天气”、“富国银行”、“世界星巴克”, “沃尔格林”、“维基百科”、“白页”、“世界杯”、“webmd”, “天气雷达”]

然而,显示的建议显示“未定义”而不是真实的单词。 你知道我在这里遗漏了什么吗?谢谢

    <input type="text" class="typeahead" placeholder="Search">


    $('.typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        source: function (query, process) {
            $.getJSON("Home/Suggest", { query: query }, function (data) {
                return process(data);
            });
        }
    });


$('.typeahead')。typeahead({
提示:没错,
推荐理由:没错,
最小长度:1
},
{
来源:功能(查询、流程){
$.getJSON(“Home/Suggest”,{query:query},函数(数据){
返回过程(数据);
});
}
});
更新:

经过一些研究,我找到了我问题的答案,如果有人需要,我会把它贴在这里

诀窍是,“process”回调函数希望结果的格式为:

[{value:“string1”},{value:“string2”},{value:“string3”}]

而不仅仅是字符串数组

$('.typeahead').typeahead(
{ hint: true, highlight: true, minLength: 1 }, // options
{
    source: function (query, process) { // source dataset, data = array of strings
        $.getJSON('Home/Suggest', { query: query }, function (data) {
            //data=["string1", "string2", "string3"]
            //process callback function needs it 
            //in a format [{value: "string1"}, {value: "string2"}, {value: "string3"}]
            var output = $.map(data, function (string) { return { value: string }; });
            process(output);
        });
    }
});

谢谢在typeahead文档中也有旧版本。我得到了相同的错误,我正在遵循示例。我的源代码是substringMatcher(states),states是一个数组。修复了损坏的初始示例是,第一个示例已损坏,
匹配.push(str)
应该是
匹配的.push({value:str})