I am trying to implement the jquery autocomplete with a node rest service, but I can get it work.
This my source code:
Autocomplete:
$('#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);
}
});
}
});
Node service
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 appears undefined in the console and the autocomplete does not work
if someone needs it
index.js
autocomplete.js
jquery
Source Code: Link