Send data from server(nodejs) to client(ajax request) Send data from server(nodejs) to client(ajax request) mongoose mongoose

Send data from server(nodejs) to client(ajax request)


If I understood it correctly, you want to reply back to client with the original id you got in the request as well as the newly generated id.What I didn't understand is, why are you using redirect if you don't want any page reload?

You could simply,

var obj = {    tid: req.body.tid,    _id: stuff._id};res.send(JSON.stringify(obj));

And, the ajax success handler would be

success: function(data) {    var obj = JSON.parse(data);    var id = obj._id;    doStuff(id);}

I guess this is what you want.

Edit:

As pointed by deitch, express itself converts the object into JSON string and adds a Content-Type header for JQuery to correctly identify and parse the string into JSON object. So no need to stringify and parse the data.