Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormData.append("key", "value") is not working

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 :/

like image 419
netzaffin Avatar asked Oct 13 '11 09:10

netzaffin


People also ask

How do I add values to a FormData?

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.

Why is my form data empty?

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.

Can I append object in FormData?

Yes you can, you can append to formData objects.

Why FormData is empty in jquery?

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....


2 Answers

New in Chrome 50+ and Firefox 39+ (resp. 44+):

  • formdata.entries() (combine with Array.from() for debugability)
  • formdata.get(key)
  • and more very useful methods

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.

like image 88
Rudie Avatar answered Oct 10 '22 02:10

Rudie


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.

like image 44
Jesper Avatar answered Oct 10 '22 02:10

Jesper