我想要实时搜索框如何使用 nodejs 和 sqlite 数据库连接前端和后端?

问题描述

我需要来自服务器的实时响应而不刷新页面我不明白代码帮助有什么问题

app.post('/search',function(req,res){
        var str = {
            stringPart:req.body.typeahead
        }
        console.log(str)

db.all(`SELECT first_name FROM user_name WHERE first_name LIKE "%${str.stringPart}%"`,function(err,rows,fields) {
        if (err) throw err;
        var data=[];
        for(i=0;i<rows.length;i++)
        {
            data.push(rows[i].first_name);
        }
        res.send(JSON.stringify(data));
    });
});

ajax 代码

$(document).ready(function(){
    $('input.typeahead').typeahead({
    name: 'typeahead',remote: 'http://localhost:3000/search?typeahead=%query',});
  });

HTML 代码

<form action="/search" method="POST"> 
        <label>Search Data</label>
        <input class="typeahead tt-query" spellcheck="false" autocomplete="off" name="typeahead" type="text" />
        </form>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)