How to access data on server side from AJAX post request

I have tried reading a lot of posts on this but none have really helped me. I don't get how to access the data I am sending from a post request. For context, I have a .ejs html file where I am making an asynchronous post request to the server. This is the code for that:

                $("#commentButton" + thisPostId).click(function () {
                    $.ajax({
                        type: "POST",
                        url: "/comment",
                        data: {
                            comment: $("#commentInput" + thisPostId).val(),
                            postId: $("#postIdInput" + thisPostId).val()
                        },
                        success: function (data) {
                            if (data.success) {
                                let asyncComment = "<p>whatsupbro</p>";
                                $("#li" + thisPostId).append(asyncComment);
                            } else {
                                // ADD BETTER ERROR IMPL
                                alert('error');
                            }
                        }
                    });

On the server side, I want to retrieve the arguments in "data". This is what I have so far. I have tried so many things, like doing req.data.comment, req.comment, etc. Below is the start of my node.js function that is supposed to get the request and do some stuff with it. What matters is I want the comment in commentInfo and postId in commentInfo to be what I am sending in the post request as "comment" and "postId". I really am just not sure how to access this data (req.body.mycomment doesn't work either).

var createComment = function(req, res) {
    var commentInfo = {
        comment: req.body.myComment,
        username: req.session.username,
        commentId: new Date().getTime(),
        postId: req.body.postId
    };
    console.log(req['comment']);

Thanks for the help. If there is anything else I should add let me know.



from Recent Questions - Stack Overflow https://ift.tt/2J5sNnB
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations