JavaScript
0
Вклад в тег
var form = document.getElementById('testForm');
form.onsubmit = function (e) {
e.preventDefault();
var data = {};
for (var i = 0, ii = form.length; i < ii; ++i) {
var input = form[i];
if (input.name) {
data[input.name] = input.value;
}
}
var xhr = new XMLHttpRequest();
xhr.open('POST', 'url', true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) {
alert(xhr.status + ': ' + xhr.statusText);
} else {
console.log(xhr.response);
alert(xhr.responseText);
}
}
xhr.send(JSON.stringify(data));
}