繁体   English   中英

如何从 FormData() 访问我的数据(用户名、密码等;)到我的快速服务器

[英]How to get access my data (username, password etc;) from FormData() into my express server

我目前正在尝试从我通过 POST 请求发送到我的快递服务器的 FormData 中获取我的数据的正确部分(实际的用户名值和密码值,以便我可以提取它并将其保存到一个值等;)但我似乎无法得到它,当我尝试执行req.body.usernamereq.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'

我的 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)
});

我的 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);

我的 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>

任何帮助都会很棒,提前谢谢你

尝试箭头功能。 首先在前端从您的请求记录,然后在后端从整个 req object 记录。 然后你可以找到身体

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM