update
This commit is contained in:
parent
962caebbb7
commit
87bf53d076
1825 changed files with 94929 additions and 0 deletions
0
3-more/10-ajax/2-ajax-nodejs/example/.zip
Executable file
0
3-more/10-ajax/2-ajax-nodejs/example/.zip
Executable file
35
3-more/10-ajax/2-ajax-nodejs/example/index.html
Executable file
35
3-more/10-ajax/2-ajax-nodejs/example/index.html
Executable file
|
@ -0,0 +1,35 @@
|
|||
<!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>
|
31
3-more/10-ajax/2-ajax-nodejs/example/index.js
Executable file
31
3-more/10-ajax/2-ajax-nodejs/example/index.js
Executable file
|
@ -0,0 +1,31 @@
|
|||
var http = require('http');
|
||||
var url = require('url');
|
||||
var querystring = require('querystring');
|
||||
var static = require('node-static');
|
||||
var file = new static.Server('.');
|
||||
|
||||
|
||||
function accept(req, res) {
|
||||
|
||||
// если URL запроса /vote, то...
|
||||
if (req.url == '/vote') {
|
||||
// через 1.5 секунды ответить сообщением
|
||||
setTimeout(function() {
|
||||
res.end('Ваш голос принят: ' + new Date());
|
||||
}, 1500);
|
||||
} else {
|
||||
// иначе считаем это запросом к обычному файлу и выводим его
|
||||
file.serve(req, res); // (если он есть)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ------ этот код запускает веб-сервер -------
|
||||
|
||||
if (!module.parent) {
|
||||
http.createServer(accept).listen(8080);
|
||||
} else {
|
||||
exports.accept = accept;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue