【问题标题】:Twitter Typeahead.js in Node js NOT fetching suggestionsNode js 中的 Twitter Typeahead.js 未获取建议
【发布时间】:2013-12-17 05:52:38
【问题描述】:

用于预输入的代码:

<script type="text/javascript">
  $('input.typeahead').typeahead({
    name: 'Artist',
    prefetch:{url: '/queryjson, ttl: '1'},
    template: '<p><strong>{{firstname}}</strong>',
    limit : 10,
    engine: Hogan,
  });
</script>

App.js 中的代码:

app.get('/queryjson', function(req,res,next){
  var firstname = req.body.firstname;
  connection.query("select firstname from entries",
  function (err, rows, fields) {
    if (err)
    throw err;
    res.end(JSON.stringify({
      data : rows
    }));
  });
})

最后是 HTML 中输入文本的代码:

<input class="typeahead" type="text" placeholder="Artist" data-provide="typeahead">

注意: 当我在地址栏中输入/queryjson时,数据库生成的行是可用的,并且是json格式({"data":[{"firstname":"sheila"},{"firstname":"Noreen"}...

但是当我在输入文本中输入内容时,不会生成任何建议。

您对可能出现的问题有任何想法吗?我真的,真的需要你的帮助。

或者你对使用prefetch在节点中正确实现typeahead有什么建议吗?

【问题讨论】:

  • 您可以格式化您的代码,使其更具可读性。

标签: ajax json node.js twitter-typeahead


【解决方案1】:

您缺少单引号!将'/queryjson, 替换为'/queryjson',

除此之外,它看起来是正确的,http://jsfiddle.net/8GJh2/

编辑:我也注意到你的 json 格式不同,they do not include 'data' 元素

查询json:

[
   { "firstname": "sheila"},
   { "firstname":"Noreen"}
]

App.js

res.end(rows);

【讨论】:

  • 谢谢!但我仍然无法生成建议。当我输入一些东西时,什么都没有发生。真的很令人沮丧。
  • 编辑我的答案,你的json中不需要data:
  • 您已经进行了编辑。 :) 但我希望你对我真的很有耐心。甚至可以在预输入代码中使用 url '/queryjson' 吗?似乎它并没有真正获取行。因此,不会生成任何建议。
猜你喜欢
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多