-2

So, when i try to append a data to a formData, it does nothing. Js code:

document.querySelector("#regForm").addEventListener('submit', e => {
    e.preventDefault();
    let username = document.querySelector("#username").value;

    let formData = new FormData();
    formData.append('username', 'username');
    console.log(formData)
    
    const request = new XMLHttpRequest();
    request.open("POST", '/register');
    request.send(formData);
})

Can you help me? Thanks!

Edit: Screenshot

Rezso
  • 1
  • 2

2 Answers2

1

check this formData.get('username') // "username" it is working

0

You can use FormData.entries

Since FormData.entries return iterator

You can transform it to array using spread operator

If you want the result to be object you need to use the reduce function

const formData = new FormData()

formData.append('username', 'username')
formData.append('password', 'password')

const result = [...formData.entries()].reduce((acc, val) => {
  acc[val[0]] = val[1]

  return acc
}, {})

console.log(result)
kennarddh
  • 2,186
  • 2
  • 6
  • 21