FormData()
The FormData()
constructor creates a new FormData
object.
Syntax
new FormData()
new FormData(form)
Note:
FormData()
can only be constructed withnew
. Attempting to call it withoutnew
throws aTypeError
.
Parameters
form
optional- : An HTML
<form>
element — when specified, theFormData
object will be populated with the form's current key/value pairs using the name property of each element for the keys and their submitted value for the values. File input elements are handled specially: their values are taken from the files selected by the user in the upload control.
- : An HTML
Return value
A new FormData
object, pre-populated with form data if the optional form
parameter was provided.
Description
The FormData
interface provides a way to construct a set of key/value pairs representing form fields and their values, which can be sent using methods such as fetch()
. It uses the same format a form would use if the encoding type were set to "multipart/form-data"
.
You can also append additional data to the FormData
object after it's created using its various methods.
A FormData
object can be used in a number of ways with other APIs:
- It can be sent with the
fetch()
API - It works seamlessly with the
Request
andResponse
objects - it can be used directly as the body of aRequest
object - It can be obtained from a
Response
object using theformData()
method - It can be passed directly to the
URLSearchParams
constructor
The transmitted data is in the same format that the form's submit()
method would use to send the data if the form's encoding type were set to "multipart/form-data"
.