【问题标题】:typeahead and playframework alert displays the data but the suggestion comes as undefinedtypeahead 和 playframework alert 显示数据,但建议未定义
【发布时间】:2014-06-12 01:15:59
【问题描述】:

我正在尝试将 Typeahead 与 Bootstrap 3 一起用于搜索框上的自动完成功能。

当我对服务器进行 Ajax 调用时,我会以 Json 的形式返回响应。当我将该响应传递给预先输入的过程时,我得到的建议是未定义的。但如果我打印它控制台或警报,我会看到从服务器返回的数据。

下面是代码

javascript 代码

$('#search-box .typeahead').typeahead({

  hint: true,
  highlight: true,
  minLength: 1
},
{
  source: function (query, process) {
      return $.ajax({
            url: "/type_assist/" + query,
            type: "GET",
            dataType: "JSON",
            async: false,
            success: function (data) {
                alert(data)
                return typeof data == 'undefined' ? false : process(data);
            }
        }); 
  }

});

以及处理java脚本中的url的java代码。我正在使用 import play.libs.Json;

SortedSet<String> set =  CityZipTypeAssist(charInput);      
        return ok(Json.toJson(set));

谢谢

【问题讨论】:

    标签: json twitter-bootstrap playframework twitter-bootstrap-3 typeahead.js


    【解决方案1】:

    我能够解决这个问题.. javascript 中的一些更改必须将响应添加到数组对象中,然后将其传递给下面的过程是代码

        displayKey: 'value',
      source: function (query, process) {
          return $.ajax({
                url: "/type_assist/" + query,
                type: "GET",
                dataType: "JSON",
                async: false,
    
                success: function (data) {
                    var suggestions = [];
                      $.each(data, function(key, val) {
                          var obj = {};
                          obj.label = val;
    
                          suggestions.push({ value: val });
                  });
                      return typeof data == 'undefined' ? false : process(suggestions);
                }
            }); 
      }
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      相关资源
      最近更新 更多