I'm currently trying to get the correct part of my data (the actual Username Value and Password Value by itself so I can extract it and save it to a value etc;) from FormData which I sent through a POST request to my express server but I can't seem to get it, it either comes back undefined when I try doing req.body.username OR req.body.name or prints all of my FormData when I do req.body:
'------WebKitFormBoundarymqLPaiCoVje2rgBB\r\nContent-Disposition: form-data; name': '"username"\r\n' +
'\r\n' +
'myNamePutInHere\r\n' +
'------WebKitFormBoundarymqLPaiCoVje2rgBB\r\n' +
'Content-Disposition: form-data; name="password"\r\n' +
'\r\n' +
'password123\r\n'
My Express Server: (NOTE: I also tried req.body.username but that didn't work either)
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/', function(req, res) {
res.send(req.body.name)
console.log(req.body.name)
});
My JS to send it:
const form = document.getElementById( "signUpForm" );
const XHR = new XMLHttpRequest();
const FD = new FormData( form );
XHR.open( "POST", "http://localhost:3001/", true);
XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
XHR.send(FD);
My HTML form:
<form id="signUpForm" name="signUpForm">
<div class="flex flex-col">
<input type="text" name="username" placeholder="Username*" class="w-10/12 mr-2 h-7 border-2 mb-3 rounded-lg py-4 px-3 signupinput" required>
<input type="password" name="password" placeholder="Password*" class="w-3/6 h-7 border-2 mr-2 rounded-lg py-4 px-3 signupinput" required>
</div>
<button class="w-40 h-10 text-white bg-main-blue hover:bg-blue-300 rounded-lg mt-12" type="submit" value="Submit">Create an account</button>
</form>
Any help would be great, Thankyou in advance
try arrow functions. log from your request at frontend first then log from whole req object on backend. then you can find body