en.javascript.info/4-ajax/2-ajax-nodejs/example/index.html
2015-02-27 13:21:58 +03:00

35 lines
642 B
HTML

<!DOCTYPE HTML>
<html>
<head><meta charset="utf-8"></head>
<body>
<button onclick="vote()" id="button">Голосовать!</button>
<script>
function vote() {
button.innerHTML = ' ... ';
var xhr = new XMLHttpRequest();
xhr.open('GET', 'vote', true);
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) {
// обработать ошибку
alert('Ошибка ' + xhr.status + ': ' + xhr.statusText);
return;
}
// обработать результат
button.innerHTML = xhr.responseText;
}
xhr.send(null);
}
</script>
</body>
</html>