logo search
Vvedenie_v_Veb_programmirovanien_2 / Vvedenie_v_Veb_programmirovanien_2

Объект FormData

One of the enhancements to XMLHttpRequest is the introduction of the FormData object. With the FormData object, you can create and send a set of key/value pairs and, optionally, files using XMLHttpRequest. When using this technique, the data is sent in the same format as if you'd submitted it via the form's submit() method with the encoding type of multipart/form-data.

FormData позволяет создать форму HTML на лету, используя JavaScript, и затем переслать ее, используя XMLHttpRequest.send(). Простой пример:

var formData = new FormData();

formData.append("part_num", "123ABC");

formData.append("part_price", 7.95);

formData.append("part_image", somefile)

var xhr = new XMLHttpRequest();

xhr.open("POST", "http://some.url/");

xhr.send(formData);

You can also use FormData to add additional data to an existing form before submitting it.

var formElement = document.getElementById("someFormElement");

var formData = new FormData(formElement);

formData.append("part_description", "The best part ever!");

var xhr = new XMLHttpRequest();

xhr.open("POST", "http://some.url/");

xhr.send(formData);