0
<form action="" method="post">
    <div class="bottom_wrapper clearfix">
    <div class="message_input_wrapper" id="eventForm">

    <input class="message_input" name="msg" placeholder="Type your message here..." />
        </div>

    <input type="submit" name="submit">
        <div>

</form>   

<script>
    $('#eventForm').submit(function (e) {
        e.preventDefault();
        var fd = new FormData($(this)[0]);
        $.ajax({
            url: '/messages/'<%= id %>,
            data: fd,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function(data){
                console.log(data);
            }
        });
    });

</script>

The above code I am using in front end and in the node.js controller

 BASE.APP.post('/messages/:id', function(req,res)
     {
      var body = req.body;
         console.log('postdata',body);

     });

While post the data it's give me blank.
Actually it's a chat msg window where I want to send msgs without refresh the page.

1
  • What is happening right now ? Is console giving you expected results ? Commented Apr 19, 2016 at 7:10

1 Answer 1

1

The form doesn't have the ID eventForm, that's a DIV, which doesn't have a submit event.

Add the ID to the form instead of the DIV.
Also, you have a typo, one of the closing </div> tags aren't really closing anything, but opening.

<form action="" method="post" id="eventForm">
    <div class="bottom_wrapper clearfix">
        <div class="message_input_wrapper">
            <input class="message_input" name="msg" placeholder="Type your message here..." />
        </div>
        <input type="submit" name="submit">
    </div>
</form>   

<script type="text/javascript">
      $('#eventForm').submit(function (e) {
          e.preventDefault();
          var fd = new FormData(this);

          $.ajax({
               url: '/messages/'<%= id %>,
               data: fd,
               processData: false,
               contentType: false,
               type: 'POST',
               success: function(data){
                   console.log(data);
               }
          });
     });
</script >
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.