The 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 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) is equivalent to for (var p of myFormData.entries()).
# Note: The fields "userfile" and "webmasterfile" both contain a file. The number assigned to the field "accountnum" is immediately converted into a string by the FormData.append() method (the field's value can be a Blob, File, or a string: if the value is neither a Blob nor a File, the value is converted to a string).
var formData = new FormData();
formData.append("username", "Groucho"); formData.append("accountnum", 123456); // number 123456 is immediately converted to a string "123456"
// HTML file input, chosen by user formData.append("userfile", fileInputElement.files[0]);
// JavaScript file-like object var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file... var blob = new Blob([content], { type: "text/xml"});
formData.append("webmasterfile", blob); // formData.append("webmasterfile", blob, "filename.txt"); // When no filename is specified (or the parameter isn't supported), the name "blob" is used.
var request = new XMLHttpRequest(); request.open("POST", "http://foo.com/submitform.php"); request.send(formData);
Retrieving a FormData object from an HTML form
1 2 3 4 5 6 7 8 9 10
# FormData will only use input fields that use the name attribute.
var formData = new FormData(someFormElement);
var formElement = document.querySelector("form"); var formData = new FormData(formElement); var request = new XMLHttpRequest(); request.open("POST", "submitform.php"); formData.append("serialnumber", serialNumber++); request.send(formData);
formElem.addEventListener('submit', (e) => { // on form submission, prevent default e.preventDefault();
// construct a FormData object, which fires the formdata event new FormData(formElem); });
# Note: The formdata event and FormDataEvent object are available in Chrome from version 77 (and other equivalent Chromiums), and Firefox 72 (first available behind the dom.formdata.event.enabled pref in Firefox 71). formElem.addEventListener('formdata', (e) => { console.log('formdata fired');
// Get the form data from the event object let data = e.formData; for (var value of data.values()) { console.log(value); }
// submit the data via XHR let request = new XMLHttpRequest(); request.open("POST", "/formHandler"); request.send(data); });
1 2 3 4 5 6 7 8 9 10
// jquery use FormData var fd = new FormData(document.querySelector("form")); fd.append("CustomField", "This is some extra data"); $.ajax({ url: "stash.php", type: "POST", data: fd, processData: false, // 不处理数据 contentType: false// 不设置内容类型 });