Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get All Items from HTML5 FormData Object - HTML5

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:

enter image description here

like image 625
karns Avatar asked Mar 06 '15 19:03

karns


People also ask

How get all values from FormData?

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 can I convert FormData to JSON?

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.

What is Fromdata?

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.


1 Answers

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.

like image 195
originalhat Avatar answered Sep 23 '22 03:09

originalhat