samedi 20 octobre 2018

using XmlHttpRequest,responseText in order to get node js response not working

I am trying to build a client & server using node js & ajax... I am trying to get info from my node js server but I don't get it...

site(client side):

<head>
    <title>Testing...</title>
    <script>
        function loadXMLDoc()
        {
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            //xmlhttp.addEventListener("load", server_test); <-- not working as well...
            xmlhttp.onreadystatechange = server_test;
            xmlhttp.open("GET","http://localhost:8001/?web=www.google.com", true);
            xmlhttp.send();
        }


        function server_test()
        {
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
    </script>
</head>
<body>
    <div id="myDiv"><h2>Waiting for response...</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>

server side:

var http = require('http'),
url = require('url');

http.createServer(function(request, response){

console.log("request recieved");
var web = url.parse(request.url, true).query.web;
console.log("Sending info about " + web);

response.writeHead(200, {"Content-Type": "text/plain"});
response.write(web)
response.end(web);

console.log("Info sent...");


}).listen(8001);
console.log("server initialized");

I searched for hours and I still couldn't find an answer hope I will be able to find it here. Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire