en.javascript.info/4-ajax/3-ajax-xmlhttprequest/phones-async.view/index.html
2015-02-27 13:21:58 +03:00

40 lines
736 B
HTML

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