XMLHttpRequest Level 2 adds support for the new FormData interface. FormData objects provide a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest send() method.
It uses the same format a form would use if the encoding type were set to "multipart/form-data".
Note: This feature is available in Web Workers.
Note: For details on how to use the FormData object, see Using FormData objects.
Constructor
FormData()- Creates a new
FormDataobject.
Methods
FormData.append()- Appends a new value onto an existing key inside a
FormDataobject, or adds the key if it does not already exist. FormData.delete()- Deletes a key/value pair from a
FormDataobject. FormData.get()- Returns the first value associated with a given key from within a
FormDataobject. FormData.getAll()- Returns an array of all the values associated with a given key from within a
FormData. FormData.has()- Returns a boolean stating whether a
FormDataobject contains a certain key/value pair. FormData.set()- Sets a new value for an existing key inside a
FormDataobject, or adds the key/value if it does not already exist.
Note: To be clear, the difference between FormData.set() and FormData.append() is that if the specified key does already exist, FormData.set() will overwrite the existing value with the new one, whereas FormData.append() will append the new value onto the end of the existing values. See their dedicated pages for example code.
Specifications
| Specification | Status | Comment |
|---|---|---|
| XMLHttpRequest The definition of 'FormData' in that specification. |
Living Standard | FormData defined in XHR spec |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 7[2] | 4.0 (2.0)[1] | 10 | 12 | 5 |
| append with filename | (Yes) | 22.0 (22.0) | ? | ? | ? |
| delete, get, getAll, has, set | Behind Flag | Not supported | Not supported | (Yes) | Not supported |
| Available in web workers | (Yes) | 39.0 (39.0) | Not supported | (Yes) | Not supported |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Basic support | 3.0 | ?[3] | 4.0 (2.0)[1] | 1.0.1 | ? | 12 | ? |
| append with filename | ? | ? | 22.0 (22.0) | 1.2 | ? | ? | ? |
| delete, get, getAll, has, set | (Yes) | (Yes) | Not supported | Not supported | Not supported | (Yes) | Not supported |
| Available in web workers | (Yes) | (Yes) | (Yes) | (Yes) | Not supported | (Yes) | Not supported |
[1] Prior to Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4), if you specified a Blob as the data to append to the object, the filename reported in the "Content-Disposition" HTTP header was an empty string; this resulted in errors being reported by some servers. Starting from Gecko 7.0, the filename "blob" is sent.
[2] Chrome support for methods other than append is currently behind the "Enable Experimental Web Platform Features" flag.
[3] XHR in Android 4.0 sends empty content for FormData with blob.