<html>
<body>
<script>
function test(){
alert("Inside test");
var request = new XMLHttpRequest();
request.open('GET', "http://localhost:8080/name.txt", true);
request.onreadystatechange = function(){
alert(request.responseText);
}
request.send();
}
test();
</script>
</body>
</html>
In the above code snippet, I am trying to send GET request to the local server which is hosted using node.js and it has an associated server js file to handle the request as below
var http = require('http');
var URL = require('url');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var q = URL.pathname;
res.end(q);
}).listen(8080);
I need to get the response data back to my function test and use it to display.But I am not able to do that.I can only output the response to the html page when I navigate to URI "http://localhost:8080/name.txt" manually. How do I achieve this from my code?