I have been working with the HTML5 FormData object and I can't seem to figure out how to find what data it holds. I need to access it for debugging purposes.
https://developer.mozilla.org/en-US/docs/Web/API/FormData1
There are functions like
FormData::get([name]);
but I don't know the names. It would be nice to have something like the following:
FormData::dumpData();
What is a good way to view all of the data in a FormData object?
Update
Here is an example of the FormData object:
getAll() The getAll() method of the FormData interface returns all the values associated with a given key from within a FormData object. Note: This method is available in Web Workers.
How to Convert Form Data to JSON With Object. fromEntries() Note: For both methods, we can use JSON. stringify() to convert the object into a JSON string, which we can then use to send JSON encoded data to APIs.
FormData objects are used to capture HTML form and submit it using fetch or another network method. We can either create new FormData(form) from an HTML form, or create an object without a form at all, and then append fields with methods: formData.
All the functions for FormData
aren't available in all browsers by default. However, you can enable them in some. Checkout the docs for browser compatibility.
In Chrome, you'll be able to enable full access to these functions by enabling Enable experimental Web Platform feature under the browser flags section. Navigate to chrome://flags/
in the browser to find this option.
Once you've enable this feature and reloaded, you should be able to run the following to view all your FormData
, data:
var formData = new FormData(this);
var formKeys = formData.keys();
var formEntries = formData.entries();
do {
console.log(formEntries.next().value);
} while (!formKeys.next().done)
Granted, there are probably better ways to iterate over this data structure, although it is certainly one way to view all the K/V pairs in your FormData
object dynamically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With