Java Reference
In-Depth Information
xhr.send(data);
}
We now require a function named
submitHero()
, which is called when the form is
submitted. In this function we grab a reference to the form using the
event.target
property (since it was the form that was submitted) and create a new instance of the
FormData()
constructor function, providing the form as an argument. This does all the
hard work for us; after this, we submit the form using Ajax as before, but provide the form
data instances (stored in the
data
variable) as an argument of the
send()
method.
It's also possible to add data to the form data instance as key-value pairs using the
ap-
pend()
method:
data = new FormData(); // no form provided as an argument
creates an
↵
empty form data instance
data.append("height", 75);
The
FormData
interface interface really comes into its own when a form contains files to
upload. This was a notoriously difficult task in the past, often requiring the use of Flash or
another third-party browser plugin to handle the upload process. The
FormData
instance
will automatically create the necessary settings required and take care of all the hard work
if any file uploads are present in the form.
Craig Buckler
and on the
Mozilla Developer Network.
