Socket.io access-control-allow=来自远程站点的起源错误
我正在尝试从另一个站点访问 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
这是我与套接字连接的方式:
socket = new io.Socket(
'site2.nodester.com', {port: 80, rememberTransport: false}
);
这是服务器代码:
// requires
server = http.createServer(function(req, res){
// server stuffs
}),
server.listen(8362);
var io = io.listen(server),
// io code
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一个简单的解决方法(感谢 mattcodez):
io = io.listen(8888);
io.server.removeListener('request', io.server.listeners('request')[0]);
此问题有一个未解决的错误,您可以阅读有关它的更多信息 此处:
A simple workaround (thanks to mattcodez):
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:
我通过更改一行客户端代码解决了这个问题。
我
改为
I solved this by changing one line of client code.
I changed
to
您是否配置 socket.io 仍使用默认
origins = *
或至少输入origins = site1.heroku.com
Did you configure socket.io to still use default
origins = *
or at least putorigins = site1.heroku.com
我面临的问题是,从不同的位置为客户端 socket.io.js 提供服务。
您可以通过从您尝试连接的同一服务器提供客户端 js 文件来避免此问题。
例如,我最初的客户端代码是这样的,
一旦我将其修改为这样,它就会抛出错误,它工作正常。
我的服务器代码是,
The problem I faced was, serving the client socket.io.js from a different location.
You can avoid this issue by serving the client js file from the same server where you are trying to connect to.
for example, my initial client code was this and it was throwing error
once I modified it to this, it worked alright.
And my server code is,
我有同样的问题,但没有得到解决。
我测试了很多配置:
或者
或者
谢谢你的帮助:)
I've a same problem and I don't arrive at resolved.
I test many configuration:
or
or
thanks your help :)
它可能不完全相关,但最终可能会在这个问题上帮助一些可怜的人:
我刚刚遇到从本地磁盘运行客户端(至少在 Windows 上),例如。
file:///c:/...
导致“null”源,这将使源检查崩溃。尝试将客户端上传到远程网站并从那里启动它。将原点保留在 : 上。希望这可以帮助一些人。
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 :.Hope this helps some people out.
这对我有用
This worked for me
我通过从服务器本身加载客户端的 socket.io.js 解决了这个问题,而不是在站点上维护本地副本。在我的设置中,某个网站上有一个网页可以从位于不同位置的服务器获取文件。
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.