如何在节点js中使用jquery自动完成

时间:2013-07-30 21:05:45

标签: jquery rest service autocomplete

我正在尝试使用节点休息服务实现jquery自动完成,但我可以让它工作。

这是我的源代码:

自动完成:

$('#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 在控制台中显示为未定义且自动填充功能不起作用

1 个答案:

答案 0 :(得分:13)

如果有人需要它

<强> 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) {

        }
    });

源代码: Link