小编use*_*687的帖子

在 Firefox 中运行 node.js 和 socket.io 时出现 CORS 错误

我在 apache 上使用 socket.io 运行 node.js。我让它在一个子域上运行(例如 - myapp.mydomain.com:8000),你可以看到我在不同的端口上运行它,以便我可以使用 myapp.mydomain.com 访问它。

在 chrome、Opera、IE 中一切正常,但 FF 失败并且出现跨域错误。

跨域请求被阻止:同源策略不允许在http://myapp.mydomain.com:8000/socket.io/?EIO=2&transport=polling&t=1429260923523-0读取远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。

有任何想法吗?

更新:

按要求添加了我的代码:

function handler(req, res) {
  fs.readFile('../index.html', function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(500);
      return res.end('Error loading client.html');
    }
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.writeHead(200);
    res.end(data);
  });
}
Run Code Online (Sandbox Code Playgroud)

javascript sockets firefox node.js

6
推荐指数
1
解决办法
2580
查看次数

标签 统计

firefox ×1

javascript ×1

node.js ×1

sockets ×1