JQuery自动完成不显示搜索结果

时间:2014-12-11 14:55:01

标签: javascript jquery mysql node.js

请使用JQuery Autocomplete从数据库中检索的客户列表中执行搜索。当我将数组作为源传递但是当我将源更改为从mysql数据库查询获得的结果时,它可以正常工作。我已经经历了其他几个相关问题以找到解决方案,但似乎没有与我的相关,请问我做错了什么 这是我的代码

     app.get('/SendFriendRequest', function (req, res) {

        dbconnection.query('SELECT CONCAT(FirstName," ",LastName," ",MiddleName) As FullName from Customers where FirstName like "' + req.query.term + '"%', function (err, rows, fields) {
            if (err) throw err;
            var data = [];
            for (var i = 0; i < rows.length; i++) {
                data.push(rows[i]);
            }
            var results=JSON.stringify({results:data})

            res.end(query.callback+"("+results +")");
            console.log("\tResponded with '" + results + "'\n");
        });
    })

And at the client side, I have this for my JQuery Autocomplete
      <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script>
    $(function () {

        $("#tags").autocomplete({
            // req: request has all the outgoing info (to server)
            // res: response is a callback used by jQueryUI's autocomplete
            source: function (req, res) {
                $.ajax({
                    url: "http://localhost:3000/SendFriendRequest/",
                    // JSONP to talk to localhost (where my Node server is)
                    dataType: "jsonp",
                    type: "GET",
                    data: {
                        term: req.term // term is the value of search input
                    },
                    // data has all the incoming info (from server)
                    success: function (data) {
                        // there is trickery here for non-jQuery ninjas,
                        //     see below for an explanation
                        res($.map(data.results, function (item) {
                            return {
                                label: item.value,
                                value: item.value
                            };
                        }));
                    },
                    // typical ajax error handler, nothing to see here
                    error: function (xhr) {
                        alert(xhr.status + " : " + xhr.statusText);
                    }
                });

            }
        })
    });

</script>
    <div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags">
</div>

0 个答案:

没有答案