1

Hi so I have a jquery post data that i'm sending:

                 $.ajax({
                    type: "POST",
                    url: app.config.backend_2 + '/notifications/usernames',
                    data: data,
                    contentType: 'application/javascript',
                    dataType: 'json',
                    success: function (result) {
                        console.log(result);
                    }
                });

and this is my express receiver:

 exports.save_usernames_for_notifications = function (req, res, next) {
    var start = function () {
       console.log(req);
    };

     start();

};

What do I do to get the data from the ajax to log it in the save_username_for_notifications function?

1
  • change your content type to application/json unless you want to use JSONP? Commented Jun 2, 2015 at 4:40

1 Answer 1

1

You need a body parser middleware in your expressjs application to parse your JSON req object

https://www.npmjs.com/package/body-parser

To configure it, you need this code

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // this is used for parsing the JSON object from POST

Then to get your data in your express application just do this

console.log(req.body.data)
Sign up to request clarification or add additional context in comments.

1 Comment

Yep this worked. put it on the very top after requiring the body-parser. Thanks a ton :)

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.