0

Im building a web page with node,express and mongodb. Im trying to make a like button, but the problem is I cant pass a variable value from my ejs to my server side. how can I do that? here's what I've tried Exemple

Html
 <div class="thumbnail">
           <div class="caption">
                  <strong  id="Title"><%=video.title %> </strong>
               <button id="like" value="Like" type="submit"> 
              <script>
                     var videoId =<%= video._id %>;
                 $('#like').click(function(){
                 $.post('/like/'+videoId);
               });
            </script>      

Server side

    router.post('/like/:videoId', function (req, res) {
    var test = req.params.videoId;
    console.log("works");
    console.log(test);

     });

thanks in advance

Bruno Henrique
  • 131
  • 4
  • 11

1 Answers1

0

POST-parameters are sent in the request body. To recieve a parameter from a POST-request you would need a body-parser and then grab it with req.body.videoId.

If you want to grab a parameter from the URL, like you are trying to do here, you should use a GET-request instead. Then you will be able to grab the id with req.params.videoId.

So simply change the jQuery from POST to GET and change to router.get on the server side and it should work.

Further reading: How are parameters sent in POST-request?

Community
  • 1
  • 1
tomtom
  • 642
  • 7
  • 12