Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
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:

'------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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

try arrow functions. log from your request at frontend first then log from whole req object on backend. then you can find body

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda