Can you tell me whats wrong with this:
var formdata = new FormData(); formdata.append("key", "value"); console.log(formdata);
My output looks like this, I cant find my "key" - "value" pair
FormData *__proto__: FormData **append: function append() { [native code] } ***arguments: null ***caller: null ***length: 0 ***name: "append" ***prototype: append ***__proto__: function Empty() {} *constructor: function FormData() { [native code] } **arguments: null **caller: null **length: 0 **name: "FormData" **prototype: FormData **toString: function toString() { [native code] } *__proto__: Object **__proto__: Object **__defineGetter__: function __defineGetter__() { [native code] } **__defineSetter__: function __defineSetter__() { [native code] } **__lookupGetter__: function __lookupGetter__() { [native code] } **__lookupSetter__: function __lookupSetter__() { [native code] } **constructor: function Object() { [native code] } **hasOwnProperty: function hasOwnProperty() { [native code] } **isPrototypeOf: function isPrototypeOf() { [native code] } **propertyIsEnumerable: function propertyIsEnumerable() { [native code] } **toLocaleString: function toLocaleString() { [native code] } **toString: function toString() { [native code] } **valueOf: function valueOf() { [native code] }
I can't understand! Yesterday it worked so well, and today my head crashed the keyboard so many times! Firefox, Chrome, both the same :/
append() The append() method of the FormData interface appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.
Solution. When you initialize FormData() from a form element, it'll only map input fields that have a name attribute. Only these mapped input fields are included in the form data that is sent in requests. Hence, when none of the input fields are mapped, the form data will be empty.
Yes you can, you can append to formData objects.
FormData wil support multi file upload. Should work great! NOTE: You may find that the FILES array is empty on server side page - In this case you need to make sure your server configuration allows file uploads, size limit of file upload is enough, post time is enough etc....
New in Chrome 50+ and Firefox 39+ (resp. 44+):
formdata.entries()
(combine with Array.from()
for debugability)formdata.get(key)
Original answer:
What I usually do to 'debug' a FormData
object, is just send it (anywhere!) and check the browser logs (eg. Chrome devtools' Network tab).
You don't need a/the same Ajax framework. You don't need any details. Just send it:
var xhr = new XMLHttpRequest; xhr.open('POST', '/', true); xhr.send(data);
Easy.
You say it's not working. What are you expecting to happen?
There's no way of getting the data out of a FormData
object; it's just intended for you to use to send data along with an XMLHttpRequest
object (for the send
method).
Update almost five years later: In some newer browsers, this is no longer true and you can now see the data provided to FormData
in addition to just stuffing data into it. See the accepted answer for more info.
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