Tony Eastwood
Tony Eastwood

Reputation: 853

Unexpected Access-Control-Allow-Origin error

I'm following a simple tutorial from here:

http://addyosmani.github.io/backbone-fundamentals/

I have a node.js server running on a localhost port 4711

I have tomcat running on port 8082 and a backbone.js app as client started as index.html on that server.

But I get :

XMLHttpRequest cannot load ... api/books. Origin localhost:8082 is not allowed by Access-Control-Allow-Origin.

1) Why? This is not file based access - indeed a plain web browser will see

htttp://localhost:4711/ and interact just fine.

2) What's the fix? (Given that part of this stack is a node.js server)

Upvotes: 0

Views: 1023

Answers (1)

c.P.u1
c.P.u1

Reputation: 17094

You're initiating a CORS request since the two servers are listening on different ports( index.html on localhost:8082 and your node server on localhost:4711)

In your node's http server, try setting the Access-Control-Allow-Origin header to * or to the Origin header.

http.createServer(function(req, res) {
    res.header('Access-Control-Allow-Origin', req.headers['origin']);
    //handle
});

Upvotes: 1

Related Questions