如何在 Node.js 中使用 jQuery 自动完成功能 [英] How to use jQuery autocomplete with Node.js

查看:22
本文介绍了如何在 Node.js 中使用 jQuery 自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Node REST 服务实现 jQuery 自动完成功能,但无法使其正常工作.

I am trying to implement the jQuery autocomplete with a Node REST service but can't get it work.

这是我的源代码:

自动完成:

$('#search').autocomplete({
        source: function (req, res) {
            $.ajax({
                url: "http://www.example.com:3000/autocomplete",
                dataType: "jsonp",
                type: "GET",
                data: {
                    term: req.term 
                },
                success: function (data) {
                    res($.map(data.results, function (item) {
                        return {
                            label: item.id,
                            value: item.id
                        };
                    }));
                },
                error: function (xhr) {
                    alert(xhr.status + ' : ' + xhr.statusText);
                }
            });
        }    
    });

节点服务:

exports.find = function(req, res) {
var b=req.params.term;
console.log(b);
db.collection('publication', function(err, collection) {
      collection.find({type:'pub',content: new RegExp(b, 'i') }).limit(5).toArray(function(err, items) {
                res.jsonp(items);
            });
        });
};

b 在控制台中显示为未定义且自动完成功能不起作用.

b appears undefined in the console and the autocomplete does not work.

推荐答案

如果有人需要它

index.js

var express = require('express'),
autocomplete=require('./routes/autocomplete');


var app = express();

app.configure(function () {
 app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
 app.use(express.bodyParser());
});


app.get('/autocomplete/:search',autocomplete.find);

app.listen(6000);
console.log('Listening on port 3000...');

autocomplete.js

var mongo = require('mongodb');

var Server = mongo.Server,
        Db = mongo.Db,
        BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('redsocial', server);

db.open(function(err, db) {
    if (!err) {
        console.log("Connected to 'mydb' database");
        db.collection('publication', {strict: true}, function(err, collection) {
            if (err) {
                console.log("error");
            }
        });
    }
});


exports.find = function(req, res) {
var b=req.params.search;
db.collection('publication', function(err, collection) {
      collection.find({type:'pub',content: new RegExp(b,'i')}).limit(5).toArray(function(err, items) {
                res.jsonp(items);
            });
        });
};

jquery

$('#search').autocomplete({
        source: function(req,res) {
            $.ajax({
                url: "http://www.ejemplo.com:3000/autocomplete/"+req.term,
                dataType: "jsonp",
                type: "GET",
                data: {
                    term: req.term
                },
                success: function(data) {
                    res($.map(data, function(item) {
                        return {
                            label: item.text,//text comes from a collection of mongo
                            value: item.text
                        };
                    }));
                },
                error: function(xhr) {
                    alert(xhr.status + ' : ' + xhr.statusText);
                }
            });
        },
        select: function(event, ui) {

        }
    });

源代码: 链接

这篇关于如何在 Node.js 中使用 jQuery 自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆