1

I have to send some data from client side to server side on clicking a button .But when i am sending the data using my existing code i am getting the body as value of button in my post request .

   <form action="/cart"   method="POST" >
                <input type="submit" class="btn btn-sm btn-primary"  onclick=" 
    myfunction('<%= i %>')"  name="submit"    value="Add to my Cart" >
              </form>
            </div>

<script  >
   function myfunction(i) {

  console.log(i);
  var n = document.images.item(i).src;
  console.log(n)
  var el = document.querySelectorAll("#demo");
  console.log(el[i].innerText)
  var newdata ={img :n ,title:el[i].innerText}
   console.log (newdata);

  fetch("/cart", {
  method: "POST",
  headers: {
    "Content-Type": "application/json "
  },
  body: JSON.stringify(newdata)
}.then(function(response) {
  // Handle Response
  console.log(newdata)
})
 );

}
 </script>

server side

   app.post("/cart",function(req,res){
     console.log(req.body)
     });

actual output :{ submit: 'Add to my Cart' }

expected output : newdata in body (where newdata is data in function(i) which has to be passed on clicking the button )

Manav Jain
  • 21
  • 2

0 Answers0