on sending form data using fetch api as 'application/x-www-form-urlencoded' from browser to nodejs server, body-parser at node.js server not showing data sent but some object that is not understandable.
Here is the html form code:
<body>
<form action="#" id="formToSubmit">
<input type="text" name="name" value="xyz">
<input type="text" name="phone" value="7777777777">
<input type="submit" value="submit" onClick="submitForm()">
</form>
<script>
function submitForm(){
var form = new FormData(document.getElementById("formToSubmit"));
fetch("http://localhost:8080/ppum", {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: form // problem is here
})
.then(res => res.json())
.then(data => console.log(data));
}
</script>
Here is the node server code for body parser:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
Here is the output of "req.body" at server:
{"------WebKitFormBoundary5jfSRwRQ4GjgeApo\r\nContent-Disposition: form-data; name":"\"name\"\r\n\r\nxyz\r\n------WebKitFormBoundary5jfSRwRQ4GjgeApo\r\nContent-Disposition: form-data; name=\"phone\"\r\n\r\n7777777777\r\n------WebKitFormBoundary5jfSRwRQ4GjgeApo--\r\n"}
problem is how to send data as 'application/x-www-form-urlencoded' from client side.
why is the output like as shown and how to correct it.
Note:was previously using stackoverflow.com/a/37562814/3690154 and it workes. if you have some suggesting on how to convert FormData to something like in this answer that will be highly appreciated