V
V
Vladimir2017-04-08 14:09:21
JavaScript
Vladimir, 2017-04-08 14:09:21

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();
    }
};

I get the answer like this
$.ajax({
            type: 'POST',
            url: '/TODOList',
            data: params,
            success: function(data,req,res) {
                console.log(data); //undenfined
                addProject.call($target);
            }
        })

Tell me what is wrong or how to process the answer correctly? about dataType, I read that ajax itself will understand what has come, but I also tried to specify text, the result is the same - undenfined

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alex, 2017-04-08
@zhuravlev125

undenfinedbecause you are sending 204 code
try like this:
res.send(newTask.id);

T
ThunderCat, 2017-04-08
@ThunderCat

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 question

Ask a Question

731 491 924 answers to any question