0

I am unable to make an AJAX request from my .html page to my node.js server for a JSON file. I've been reading on AJAX requests, but all I am able to make out is how to display the servers responseText.

It would be great if you could help me out, it would be even better if you could link me some tutorials on this, anyway this is what I've got at this moment:

server.js

var express = require('express');
var fs = require('fs');
var app = express();

app.get('/test', function(req, res){
    var arr = new Array();
    var rd = readline.createInterface({
        input: fs.createReadStream('info.json'),
        output: process.stdout,
        terminal: false
    });
 
    rd.on('line', function(line) {
        arr.push(line);
    }).on('close', function(){
        res.send(arr);
    });
});

app.get('/', function(req, res) {
    fs.readFile('test2.html',function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });
});

app.listen(process.env.PORT || 3000);

test.html

<html>
    <head>
        <script>
            function sendAjax(){
                var xmlHttp = new XMLHttpRequest();

                xmlHttp.onreadystatechange = function(){
                    if(xmlHttp.readyState==4 && xmlHttp.status == 200){
                        console.log(xmlHttp.responseText);
                    }
                    document.getElementById("myDiv").innerHTML=xmlHttp.responseText;
                }

                xmlHttp.open("GET", "/test", true);
                xmlHttp.send();
            }
         </script>
    </head>

    <body>
        <input type="submit" onClick="sendAjax()" value="SendAjax" />
        <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    </body>
</html>

I know this may not look like much, I've been struggling with this, and the book I have (Node.js in Action) doesn't help me alot. But as I said, what I want is to display the .json info in the browser. Thx for reading

2
  • 1
    What's the problem? Do you get any errors in the JS console? Does your browser's developer tools Net tab show the HTTP request being sent? Does it show the response? Are they what you expect? Commented Feb 12, 2014 at 12:01
  • +1 to @Quentin's comment. Is it working when you open it on the browser directly? Commented Feb 12, 2014 at 12:55

1 Answer 1

1

If I understand right, you may replace

document.getElementById("myDiv").innerHTML=xmlHttp.responseText;

with

var parsed = JSON.parse(xmlHttp.responseText);
var html = '';
for (var i = 0; i < parsed.length; i++) {
    html += '<div>' + parsed[i] + '</div>';
}
document.getElementById("myDiv").innerHTML = html;

You may also try to replace

res.send(arr);

with

res.json(arr);

Update
Or maybe you just forgot to write this line in the beginning of server.js:

var readline = require('readline');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.