node.js Socket.io access-control-allow=来自远程站点的源错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6736706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Socket.io access-control-allow=origin error from remote site
提问by JRPete
I am trying to access a socket.io server from another site. It worked for a few weeks but now I keep getting the following error. It happens when accessing a server on nodester from a server on heroku. The error is:
我正在尝试从另一个站点访问 socket.io 服务器。它工作了几个星期,但现在我不断收到以下错误。从heroku上的服务器访问nodester上的服务器时会发生这种情况。错误是:
XMLHttpRequest cannot load http://site2.nodester.com/socket.io/xhr-polling//1311008802545.
Origin http://site1.heroku.com is not allowed by Access-Control-Allow-Origin.
Resource interpreted as Script but transferred with MIME type text/plain.
Uncaught SyntaxError: Unexpected identifier
Here's how I'm connecting with the socket:
这是我与套接字连接的方式:
socket = new io.Socket(
'site2.nodester.com', {port: 80, rememberTransport: false}
);
And here's the server code:
这是服务器代码:
// requires
server = http.createServer(function(req, res){
// server stuffs
}),
server.listen(8362);
var io = io.listen(server),
// io code
回答by Alfred
回答by Avinash
The problem I faced was, serving the client socket.io.js from a different location.
我面临的问题是,从不同的位置为客户端 socket.io.js 提供服务。
You can avoid this issue by serving the client js file from the same server where you are trying to connect to.
您可以通过从您尝试连接的同一服务器提供客户端 js 文件来避免此问题。
for example, my initial client code was this and it was throwing error
例如,我最初的客户端代码是这样的,它抛出错误
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');
once I modified it to this, it worked alright.
一旦我将其修改为这个,它就可以正常工作。
<script src="http://mydomain.com/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');
And my server code is,
我的服务器代码是,
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
回答by Ambidex
It might be not exactly related, but could eventually help some poor souls on this issue:
它可能并不完全相关,但最终可以在这个问题上帮助一些可怜的人:
I've just encountered that running the client from local disk (at least on Windows) eg. file:///c:/...results in a "null" origin, which will crash the origins check. Try uploading the client to a remote website and fire it from there. Leave the origins on :.
我刚刚遇到从本地磁盘(至少在 Windows 上)运行客户端的情况,例如。file:///c:/...导致“空”原点,这将使原点检查崩溃。尝试将客户端上传到远程网站并从那里启动它。将起源留在: 上。
Hope this helps some people out.
希望这可以帮助一些人。
回答by Vsplit
I've a same problem and I don't arrive at resolved.
我有同样的问题,我没有解决。
I test many configuration:
我测试了很多配置:
io.set("origins","*");
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
or
或者
io.set("origins = *");
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
or
或者
var socket = io.listen(appS1,{origins: '*:*'});
thanks your help :)
谢谢你的帮助:)
回答by Bitsian
This worked for me
这对我有用
var app = require('express')()
, server = require('http').createServer(app)
, sio = require('socket.io');
var io = sio.listen(server, { origins: '*:*' });
回答by toujamaru
I resolved the issue by loading the client's socket.io.js from the server itself, rather than maintaining a local copy on the site. In my setup, there is a webpage on a certain website that fetches the file from the server, which is at a different location.
我通过从服务器本身加载客户端的 socket.io.js 解决了这个问题,而不是在站点上维护本地副本。在我的设置中,某个网站上有一个网页可以从位于不同位置的服务器获取文件。
回答by Steve Farthing
A simple workaround (thanks to mattcodez):
一个简单的解决方法(感谢mattcodez):
io = io.listen(8888);
io.server.removeListener('request', io.server.listeners('request')[0]);
io = io.listen(8888);
io.server.removeListener('request', io.server.listeners('request')[0]);
There is an open bug for this issue and you can read more about it here:
此问题有一个未解决的错误,您可以在此处阅读更多相关信息:
回答by hansn
I solved this by changing one line of client code.
我通过更改一行客户端代码解决了这个问题。
I changed
我变了
var socket = io('localhost');
to
到
var socket = io();

