Typeahead.js没有为Node.js获取行

时间:2019-04-10 02:47:19

标签: mysql node.js typeahead

我正在尝试使用typeahead.js库创建一个下拉列表,该列表从mysql数据库中提取数据

<div id="the-basics">
  <input class="typeahead" type="text" placeholder="States of USA">
  <input class="typeahead tt-query" spellcheck="false" autocomplete="off" name="typeahead" type="text" />
</div>

<script>
  $(document).ready(function() {
    $('input.typeahead').typeahead({
      name: 'typeahead',
      remote: 'localhost:3000/search?key=%QUERY',
      limit: 10
    });
  });

  var substringMatcher = function(strs) {
    return function findMatches(q, cb) {
      var matches, substringRegex;

      // an array that will be populated with substring matches
      matches = [];

      // regex used to determine if a string contains the substring `q`
      substrRegex = new RegExp(q, 'i');

      // iterate through the pool of strings and for any string that
      // contains the substring `q`, add it to the `matches` array
      $.each(strs, function(i, str) {
        if (substrRegex.test(str)) {
          matches.push(str);
        }
      });

      cb(matches);
    };
  };

  var states = [
    'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
    'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
    'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
    'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
    'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
    'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
    'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
    'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
    'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
  ];

  $('#the-basics .typeahead').typeahead({
    hint: true,
    highlight: true,
    minLength: 1
  }, {
    name: 'states',
    source: substringMatcher(states)
  });

</script> 
</body>

我知道我的typeahead.js库可以正常工作,因为我确实获得了从本地数组中提取数据的状态的下拉列表。但是,我对于

完全没有任何了解

在我的server.js后端,我的/ search路由为

router.get('/search',function(req,res){
  pool.query('SELECT userName from users where userName like "%'+req.query.key+'%"',
             function(err, rows, fields) {
    if (err) throw err;
    var data=[];
    for(i=0;i<rows.length;i++) {
      data.push(rows[i].partName);
    }
    res.end(JSON.stringify(data));
  });
});

问题在于,在Google Chrome控制台的“网络”标签中,没有看到发出任何Ajax获取请求。

我正在遵循this教程来实现自己的目标。

0 个答案:

没有答案