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

40 lines
No EOL
718 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>