Node Express:无法使用Typeahead显示数据

时间:2019-06-03 07:13:41

标签: node.js express typeahead.js

我正在使用typeahed通过ajax提取实时数据,但返回的结果未显示在文本框中。

EJS代码:

<div class="container">
    <div class="row">
        <div class="col-md-8">
            <label for="search">Contact Search</label>
            <input class="typeahead tt-query" spellcheck="false" autocomplete="off" name="typeahead"
                type="text" />
        </div>
    </div>
</div>

AJAX代码:

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

节点代码:

app.get('/search', function (req, res) {
    var fetchSql = 'SELECT company from leads where company like "%' + req.query.key + '%"'
    dbConfig.varConnection[dbName].query(fetchSql, function (err, rows, fields) {
        if (err) throw err;
        var data = [];
        for (i = 0; i < rows.length; i++) {
            data.push(rows[i].company);
        }
        res.end(JSON.stringify(data));
    })
});

如果我从浏览器或邮递员打来的电话,我将获得完美的表现。

URL:'http://localhost:3000/search?key=com'

结果:["Violet Company","No Company","Latest Company","Today Company","Company","comapny","Company is not herer"]

以上结果应显示在预输入文本框中。

我已经按照这种方式进行了操作:https://codeforgeek.com/ajax-search-box-using-node-mysql/

0 个答案:

没有答案