Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New FormData returns empty object

I am trying to use FormData to send user-entered data in a POST request with the fetch API. The problem is that when I create a new FormData object with the form that I've created, it keeps creating an empty object - no entries/keys/values. I'm not sure why since getting the form element returns the form correctly, it is when I call new FormData(form) that I get an empty form object. From the MDN documentation, it seems that the form should be fine to set up in separate divs which my form does. Does the form data that I'm providing need to be referenced by anything other than the input ids? Can anyone suggest how I can properly create the FormData object?

Thanks!

/*
* Add event listener on form submission
*/
document.addEventListener('DOMContentLoaded', () => {
    let form = document.getElementById('calc_form');
    form.addEventListener('submit', callServer);
});

/*
* Handler for form submission. Prevents the default action and calls longop() 2 times
*/
function callServer(event) {
    event.preventDefault();
    
    var form = event.currentTarget;
    console.log(form);                //prints the html element of calc_form, the form with the event listener
    var formData = new FormData(form);
    console.log(formData);            //prints empty FormData object
    var url = form.action;
    console.log('Sending calculation request to server');

    getData(url, formData)
        .then(data => addData(data))
        .catch(error => console.error(error));
}

HTML for form

<form id="calc_form" method="POST" action="/calculate">
        <div>
            <label for="first">First Value</label>
            <input type="number" id="first" name="first">
            <br>
            <label for="second">Second Value</label>
            <input type="number" id = "second" name="second">
        </div>
        <div>
            <input type="radio" id="add" name="operator" value="add">
            <label for="add">Add</label>
            <input type="radio" id="subtract" name="operator" value="subtract">
            <label for="subtract">Subtract</label>
            <input type="radio" id="multiply" name="operator" value="multiply">
            <label for="multiply">Multiply</label>
            <input type="radio" id="divide" name="operator" value="divide">
            <label for="divide">Divide</label>
        </div>
        <div>
            <button type="submit">Submit</button>
        </div>
        <div>
            <p id="results"></p>
        </div>
</form>
like image 618
bobweirsmoustache Avatar asked Jul 17 '26 01:07

bobweirsmoustache


1 Answers

I found the problem, there were actually 2.

The first was that on the client side code above I was trying to print the FormData object. I didn't believe that it would actually print an empty object, figured that there must be something in the entries or keys that from my input fields. After changing the console.log(form) to

    for (var [key, value] of formData.entries()) { 
        console.log(key, value);
    }

As suggested by Mark, I was able to see that the formData was actually being created properly.

The other problem was that I forgot to include app.use(bodyParser.json()) in the server side. Without this, the server was unable to parse the JSON-formatted request body. Now I get the formData contents as expected.

These two problems together was leading me to believe that I was having a problem with new FormData() but turns out I was just mishandling the data.

like image 142
bobweirsmoustache Avatar answered Jul 18 '26 13:07

bobweirsmoustache



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!