I know that there was some discussion of this topic on SO, but I can not find the answer to my question. I have a web page running on a server with Aptana on localhost: 8020. The javascript on the page hits the node server on which I am running on localhost: 1337. Here is the node code:
var io = require('socket.io'); var http = require('http'); var sys = require('sys'); var json = []; var server = http.createServer(function (req, res) { var headers = {}; headers["Access-Control-Allow-Origin"] = "*"; headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS"; headers["Access-Control-Allow-Credentials"] = true; headers["Access-Control-Max-Age"] = '86400';
I process the cors request by changing the headers that I do all the time. My client code is usually new to socket.io. Here is the tag from my code:
<script src="http://cdn.socket.io/stable/socket.io.js"></script> <script> // Create SocketIO instance var socket = new io.Socket('localhost',{ port: 1337 }); socket.connect(); // Add a connect listener socket.on('connect',function() { log('<span style="color:green;">Client has connected to the server!</span>'); }); // Add a connect listener socket.on('message',function(data) { log('Received a message from the server: ' + data); }); // Add a disconnect listener socket.on('disconnect',function() { log('<span style="color:red;">The client has disconnected!</span>'); }); // Sends a message to the server via sockets function sendMessageToServer(message) { socket.send(message); log('<span style="color:#888">Sending "' + message + '" to the server!</span>'); } // Outputs to console and list function log(message) { var li = document.createElement('li'); li.innerHTML = message; document.getElementById('message-list').appendChild(li); }
When I run the code, I keep getting "XMLHTTPRequest ... Origin is not allowed by Access-Control-Allow-Origin errors." My browser is chrome. 1. Why does my browser use XMLHTTPRequest and not Websocket? 2. Why do I get an access control error when changing headers? Thank you for your help.
jhamm source share