I am trying to display my text file content on my webpage when a button is clicked. I am using AJAX to do this and am not able to figure out what's wrong with my code.
main.js
var http = require('http'),
fs = require('fs'),
process = require('child_process');
function send404(res) {
res.writeHead(404);
res.write('Error');
res.end()
}
var value;
var server = http.createServer(function (req, res) {
if (req.method == 'GET' && req.url == '/') {
var path = 'java/hello.java';
res.writeHead(200, {'content-type':'text/html'});
fs.readFile('ajax.html', 'utf8', function (err, data) {
if (err) {
return console.log(err);
} else {
res.writeHeader(200, {"Content-Type": "text/html"});
res.write(data);
}
});
}
}).listen(3000);
Here is my HTML file code ajax.html
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "aa.txt", true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>