Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS formData.append does nothing

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading