0

So, I have a JQuery Ajax client side. But now I want to handle the ajax post Node.js side.

I have my route and body parser.

app.post('/register', function(req, res){
    var firstname           = req.body.firstname;
    var lastname            = req.body.lastname;
    var email               = req.body.email;
    var password            = req.body.password;
    var confirm_password    = req.body.confirm_password;

    // Save into my database

         ...

    // Ajax callback return ???
});

In PHP I used json_encode($array) to get an ajax callback success. But how can I do that in node.js ?

5
  • JSON.stringify(array)? Commented Aug 3, 2016 at 15:02
  • Thanks for your answer, but with that I don't have any return. I set console.log('AJAX OK') inside my route and it works but the JSON.stringify returns me nothing Commented Aug 3, 2016 at 15:22
  • 1
    JSON.stringify returns json, just like json_encode does in PHP. It doesn't output anything. Are you asking how to send the json response? It looks like you're using express, so take a look at the docs for the Response. You could use: res.json( array );. Commented Aug 3, 2016 at 15:26
  • Oh yeah. res.json works perfectly. thanks Commented Aug 3, 2016 at 15:29
  • You're welcome. I made my comment into an answer since it seems to be the answer to your question anyway. Commented Aug 3, 2016 at 15:31

1 Answer 1

1

It looks you're using express and want to send a JSON response. You can use res.json for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.