Запустил node.js, прочитал статью , сделал пример и снова облом)
<!DOCTYPE HTML>
<html>
<head><meta charset="utf-8"></head>
<body>
<button onclick="vote(this)">Голосовать!</button>
<script>
function vote(outputElem) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8080/index.js', true);
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) {
// обработать ошибку
alert('Ошибка ' + xhr.status + ': ' + xhr.statusText);
return;
}
// обработать результат
outputElem.innerHTML = xhr.responseText;
}
xhr.send(null);
}
</script>
</body>
</html>
код index.js
var http = require('http');
var static = require('node-static');
var file = new static.Server('.');
http.createServer(function (req, res) {
if (req.url == '/vote') {
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
var now = new Date();
var timeStr = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
res.end('Голос принят ' + timeStr);
return;
}
file.serve(req, res);
}).listen(8080);
пробывал вместо
xhr.open('GET', 'http://127.0.0.1:8080/index.js', true);
писать (файлы в одной папке)
xhr.open('GET', 'index.js', true);
но все равно не работает .
в консоле
или
Цитата:
|
XMLHttpRequest cannot load file:///C:/nodeJSProjects/test/index.js. Cross origin requests are only supported for HTTP.
|
почему не может загрузить файл?