【问题标题】:Twitter Bootstrap Typeahead doesn't load ajax dataTwitter Bootstrap Typeahead 不加载 ajax 数据
【发布时间】:2013-02-13 14:17:39
【问题描述】:

我有以下代码来使用版本 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 找到 Bootstrap Typeahead 的详细文档,而不是在 SO 上。

提前致谢。

【问题讨论】:

    标签: jquery twitter-bootstrap bootstrap-typeahead


    【解决方案1】:

    我们发现 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}
    });
    

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 2014-05-23
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多