Tuesday, 15 April 2014

Pass parameters in a Node.js GET Ajax -



Pass parameters in a Node.js GET Ajax -

i send ajax request router function below parameters start , end respectively.

those variables influence results request.

router.get('/winnerlist', function(req, res) { var db = req.db; var start = "20140621"; var end = "20140623"; db.collection('userlist').find({"timestamp": {"$gte": start, "$lt": end}}).toarray(function (err, items) { res.json(items); }); });

this ajax call

function populatewinners() { // empty content string var tablecontent = ''; // jquery ajax phone call json $.getjson( '/users/winnerlist', function( info ) { userlistdata = data; console.log(data); // each item in our json, add together table row , cells content string $.each(data, function(){ tablecontent += '<tr>'; tablecontent += '<td><a href="#" class="linkshowuser" rel="' + this.id2 + '" title="show details">' + this.id2+ '</a></td>'; tablecontent += '<td>' + this.email + '</td>'; tablecontent += '<td>' + this.code + '</td>'; tablecontent += '<td><a href="#" class="linkdeleteuser" rel="' + this._id + '">delete</a></td>'; tablecontent += '</tr>'; }); // inject whole content string our existing html table $('#winnerlist table tbody').html(tablecontent); }); };

i have read this question, haven't figured out how create work within node.js

for requests, parameters passed in url's query string http://en.wikipedia.org/wiki/query_string . in case, ajax url should

'/users/winnerlist?start=20140621&end=20140623'

then, on server side, can utilize variables as:

req.query.start //this equals "20140621" req.query.end // equals "20140623"

ajax node.js

No comments:

Post a Comment