Typeahead.js未显示异步结果

Typeahead.js not showing async results

本文关键字:异步 结果 显示 js Typeahead      更新时间:2023-09-26

我正在搜索字段中实现typeahead.js,异步结果偶尔会显示一次,而本地结果总是有效。

我的建议后台返回的数据是JSON:

[{
    "query": "anders troelsen",
    "hits": "1197",
    "queryCount": "39"
},
{
    "query": "anders fogh jensen",
    "hits": "295",
    "queryCount": "38"
}]

在代码中,我想将上面的JSON转换成一个字符串数组,然后显示它

    var localSuggestResults = ["anders and", "anders ladekarl", "anders høg nissen"];
    var searchSuggestions = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: localSuggestResults,
        remote: {
            url: '/services/suggest?prefix=%QUERY',
            wildcard: '%QUERY',
            transform: function (suggestions) {
                var suggestionArray = suggestions.map(function(suggestion) {
                    return suggestion.query
                });
                return suggestionArray;
            }
        }
    });
    $("#query").typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        source: searchSuggestions
    });

对我做错了什么有什么建议吗?非常感谢!

响应已晚。但图书馆在这段时间里没有改变。

asyncResult在ajax选项中设置{async:true}时有效。将source设置为您自己的函数,该函数从服务器获取结果。