Answer the question
In order to leave comments, you need to log in
How to send a response to the client from the server?
Good afternoon, I'm trying to send a response to a client from a node.js server, and get it via ajax. the express documentation says "if you need to respond with data, use res.send() or res.json()." sending response via res.send()
exports.add = function (req, res, next) {
if(req.body.input_project) {
var newTask = new task();
newTask.head.head_task = req.body.input_project;
newTask.head.userID = req.user._id;
newTask.save(function (err) {
if (err)
return next(err);
res.status(204).send(newTask.id);
});
}else{
return next();
}
};
$.ajax({
type: 'POST',
url: '/TODOList',
data: params,
success: function(data,req,res) {
console.log(data); //undenfined
addProject.call($target);
}
})
Answer the question
In order to leave comments, you need to log in
undenfined
because you are sending 204 code
try like this:res.send(newTask.id);
1) believe what comes back, is there any response from the server at all (look at the browser console)
2) check complete instead of success
3) check that the script is being executed at all on the server side. And what does it output (probably an error).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question