GET request in javascript to NodeJS


I'm trying to do a simple conection (request - response) from the javascript code on a web to a server in Node.js.

I have tried to make the request as follows:

var request = new XMLHttpRequest();'GET', 'http://localhost:4444/', false);

if (request.status === 200) {

Running this code I got an error in FireBug

I have continued searching and I found that this method is only for Make ggeeett requests on the same domain. To make cross domain requests we must use other strategies.

I found a jQuery method, and it seems that i'm on the right way:

    function(data) {
        //Do anything with "data"

In this case I get the same response without the error.

It seems it works but the "alert" message is never shown! What happens? What am I doing wrong?

The Node.js server code is:

var http = require("http");

http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/html"});
Problem courtesy of: palvarez89


So you're running into cross domain issues. You have a few options:

1) since you're using node, use . It's cross domain compliant.

On the client:

<script src="Full path to were socket IO is held on your server//"></script>
    var socket = io.connect();

    socket.on('some_callback', function(data){
        // receive data

    socket.emit('some_other_callback', {'data': value}); //send data


var io = require('').listen(server);

// define interactions with client
io.sockets.on('connection', function(socket){
    //send data to client
    socket.emit('some_callback', {'data': value});

    //recieve client data
    socket.on('some_other_callback', function(data){
        //do something

2) Since you just want to use GET you can use JSONP

        //do something

Here we pass your normal GET params as well as callback=?. You will return the following from your server:

var r = url.parse(req.url,true);
r.query.callback + '(' + some JSON + ')'

3) If you don't care about all browser compatibility you can use CORS: You can see a much better example than I would be able to write Here

Solution courtesy of: Daniel Nill


I think your problem is Same Origin Policy. Your browser must get webpage from node.js instance.

Otherwise, you must use something like CORS. There also good question on SO: Ways to circumvent the same-origin policy.

Discussion courtesy of: neoascetic

Cross domain ajax requires special support from your server.

Either CORS: Which not all browsers support yet. It involves special headers in both the request and response that tell the browser that one domain is allowed to communicate with the other, and for what data.

Or JSONP: WHich will work anywhere, but has some implementation limitations. It involves the server wrapping the response in a javascript function callback that will execute and pass in that data you want.

Either way, the server needs to be setup for each of these approaches.

Discussion courtesy of: Alex Wayne

This recipe can be found in it's original form on Stack Over Flow.