FormData

FormData interface provides 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" .

You can also pass it directly to the URLSearchParams constructor if you want to generate query parameters in the way a <form> would do if it were using simple GET submission.

An object implementing FormData can directly be used in a for...of structure, instead of entries() : for (var p of myFormData) 相当于 for (var p of myFormData.entries()) .

注意 : This feature is available in Web 工作者 .

构造函数

FormData()
创建新的 FormData 对象。

方法

FormData.append()
Appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.
FormData.delete()
Deletes a key/value pair from a FormData 对象。
FormData.entries()
Returns an iterator allowing to go through all key/value pairs contained in this object.
FormData.get()
Returns the first value associated with a given key from within a FormData 对象。
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 FormData object contains a certain key.
FormData.keys()
Returns an iterator allowing to go through all keys of the key/value pairs contained in this object.
FormData.set()
Sets a new value for an existing key inside a FormData object, or adds the key/value if it does not already exist.
FormData.values()
Returns an iterator allowing to go through all values  contained in this object.

规范

规范 状态 Comment
XMLHttpRequest
The definition of 'FormData' in that specification.
实时标准 FormData defined in XHR spec

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
FormData Chrome 7 Edge 12 Firefox 4
4
Prior to Firefox 7, specifying a Blob as the data to append to the object, the filename reported in the Content-Disposition HTTP header was an empty string, resulting in errors on some servers. Starting with Firefox 7, the filename blob is sent.
IE 10 Opera 12 Safari 5 WebView Android ≤37
≤37
XHR in Android 4.0 sends empty content for FormData with blob .
Chrome Android 18 Firefox Android 4
4
Prior to Firefox 7, specifying a Blob as the data to append to the object, the filename reported in the Content-Disposition HTTP header was an empty string, resulting in errors on some servers. Starting with Firefox 7, the filename blob is sent.
Opera Android 12 Safari iOS 5 Samsung Internet Android 1.0
FormData() 构造函数 Chrome 7 Edge 12 Firefox 4 IE 10 Opera 12 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 5 Samsung Internet Android 1.0
Support of for...of Chrome 50 Edge 79 Firefox 44 IE ? Opera ? Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 44 Opera Android ? Safari iOS 11 Samsung Internet Android 5.0
append Chrome 7 Edge 12 Firefox 4
4
Prior to Firefox 7, specifying a Blob as the data to append to the object, the filename reported in the Content-Disposition HTTP header was an empty string, resulting in errors on some servers. Starting with Firefox 7, the filename blob is sent.
IE 10
10
With the "Include local directory pass when uploading files to a server" option enabled, IE will change the filename inside the Blob on the fly. To have direct control of the sent filename, the developer should send the filename as the third parameter value, i.e. formData.append(name, value, filename) .
Opera 12 Safari 5 WebView Android 3
3
XHR in Android 4.0 sends empty content for FormData with blob .
Chrome Android 18 Firefox Android 4
4
Prior to Firefox 7, specifying a Blob as the data to append to the object, the filename reported in the Content-Disposition HTTP header was an empty string, resulting in errors on some servers. Starting with Firefox 7, the filename blob is sent.
Opera Android 12 Safari iOS 5 Samsung Internet Android 1.0
delete Chrome 50 Edge 18 Firefox 39 IE No Opera Yes Safari 11 WebView Android Yes Chrome Android 50 Firefox Android Yes Opera Android Yes Safari iOS No Samsung Internet Android 5.0
entries Chrome 50 Edge 18 Firefox 44 IE No Opera 37 Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 44 Opera Android 37 Safari iOS 11 Samsung Internet Android 5.0
get Chrome 50 Edge 18 Firefox 39 IE No Opera 37 Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 39 Opera Android 37 Safari iOS 11 Samsung Internet Android 5.0
getAll Chrome 50 Edge 18 Firefox 39 IE No Opera 37 Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 39 Opera Android 37 Safari iOS 11 Samsung Internet Android 5.0
has Chrome 50 Edge 18 Firefox 39 IE No Opera Yes Safari 11 WebView Android Yes Chrome Android 50 Firefox Android Yes Opera Android Yes Safari iOS No Samsung Internet Android 5.0
keys Chrome 50 Edge 18 Firefox 44 IE ? Opera Yes Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 44 Opera Android ? Safari iOS 11 Samsung Internet Android 5.0
set Chrome 50 Edge 18 Firefox 39 IE No Opera 37 Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 39 Opera Android 37 Safari iOS 11 Samsung Internet Android 5.0
values Chrome 50 Edge 18 Firefox 44 IE No Opera 37 Safari 11 WebView Android 50 Chrome Android 50 Firefox Android 44 Opera Android 37 Safari iOS 11 Samsung Internet Android 5.0
Available in workers Chrome Yes Edge ≤18 Firefox 39 IE No Opera Yes Safari No WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS No Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
见实现注意事项。

另请参阅