I think the closest thing would be to use FormData
. Something like:
document.querySelector('form').addEventListener('submit', function (e) {
e.preventDefault();
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
}
xhr.open("POST", "/echo/json");
xhr.send(new FormData(this));
});
This is arguably even simpler than calling .serialize
on the form collection, but note that you can't inspect what is inside the FormData
object apparently. This is also relatively new, so not so cross-browser compatible.