javascript - how can i submit a form without refersh in node.js and ejs? -
<form action="" method="post"> <div class="bottom_wrapper clearfix"> <div class="message_input_wrapper" id="eventform"> <input class="message_input" name="msg" placeholder="type 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 using in front end , in node.js controller
base.app.post('/messages/:id', function(req,res) { var body = req.body; console.log('postdata',body); });
while post data give me blank. chat msg window want send msgs without refersh page.
the form doesn't have id eventform, that's div, doesn't have submit event.
add id form instead of div.
also, have typo, 1 of closing </div>
tags aren't closing anything, 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 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 >
Comments
Post a Comment