簡體   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