0

用于预输入的代码:

<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"}...

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

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

或者您对typeaheadin node using的正确实施有什么建议prefetch吗?

4

1 回答 1

0

you are missing a single quote! replace '/queryjson, with '/queryjson',

appart from that, it looks correct, http://jsfiddle.net/8GJh2/

Edit: I also notice that your json format is different, they do not include the 'data' element

queryjson:

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

App.js

res.end(rows);
于 2013-12-17T06:00:17.313 回答