socket.io:-没有'Access-Control-Allow-Origin'标题,错误

时间:2013-12-01 03:07:04

标签: connection socket.io

我的浏览器控制台在尝试与网络应用程序建立socket.io连接时显示以下错误。

XMLHttpRequest cannot load http://abc.in/socket.io/1/?t=1385867719492. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.abc.in' is therefore not allowed access. 

2 个答案:

答案 0 :(得分:0)

heroku labs:enable websockets -a myapp

来自link

或者可以添加: -

io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10); 
});

另请参阅answer

答案 1 :(得分:-1)

从您尝试连接的同一服务器上为客户端socket.io.js文件服务。

例如,我的初始客户端代码是这个,它抛出了错误

<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

一旦我将其修改为此,它就可以正常工作。

<script src="http://mydomain.com/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

我的服务器代码是,

var express = require('express');
var app = express();
app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Headers", "Content-Type");
        res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
        next();
    });
var server = http.createServer(app);
io = socketio.listen(server, {log:false, origins:'*:*'});
... //io.connect and events below