[英]How to get access my data (username, password etc;) from FormData() into my express server
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
:我目前正在尝试从我通过 POST 请求发送到我的快递服务器的 FormData 中获取我的数据的正确部分(实际的用户名值和密码值,以便我可以提取它并将其保存到一个值等;)但我似乎无法得到它,当我尝试执行
req.body.username
或req.body.name
时,它要么返回undefined
,要么在我执行req.body
时打印我的所有 FormData :
'------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)我的 Express 服务器:(注意:我也尝试
req.body.username
但也没有用)
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:我的 JS 发送它:
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:我的 HTML 表格:
<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.
首先在前端从您的请求记录,然后在后端从整个 req object 记录。 then you can find body
然后你可以找到身体
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.