Twitter Bootstrap Typeahead不加载ajax数据

时间:2013-02-13 14:17:39

标签: jquery twitter-bootstrap bootstrap-typeahead

我有以下代码使用版本2.3.0初始化typeahead:

jQuery('#search_terms').typeahead({
    source: function(query, process) {
        return jQuery.ajax({
            url: '/api/path',
            type: 'GET',
            data: {q: query},
            dataType: 'json',
            success: function (json) {
                return process(json.suggestion);
            }
        });
    }
});

我已经通过替换静态数据验证了typeahead的工作原理。我已经看到json.suggestion按预期计算单个单词。 ajax响应本身看起来像这样:

{"suggestion":"word"}

但是,Bootstrap拒绝将响应加载到typeahead。我错过了什么?我承认我在通过ajax而不是在SO上找到Bootstrap Typeahead的详细文档时遇到了麻烦。

提前致谢。

1 个答案:

答案 0 :(得分:5)

我们发现Bootstrap Typeahead使用自己的匹配过滤器来猜测我们的ajax数据。通过强制匹配为真,我们解决了这个问题:

jQuery('#search_terms').typeahead({
    source: function(query, process) {
        jQuery.ajax({
            url: 'api/path',
            type: 'GET',
            data: {q: query},
            dataType: 'json',
            success: function (json) {
                process([json.suggestion]);
            }
        });
    },
    matcher: function (param) {return true}
});
相关问题