Socket.io access-control-allow=origin error from remote site

落花浮王杯 提交于 2019-12-04 20:48:45

问题


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

回答1:


Did you configure socket.io to still use default origins = * or at least put origins = site1.heroku.com




回答2:


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

<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



回答3:


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 :)




回答4:


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.




回答5:


This worked for me

var app = require('express')()
  , server = require('http').createServer(app)
  , sio = require('socket.io');     

var io = sio.listen(server, { origins: '*:*' });



回答6:


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.




回答7:


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:




回答8:


I solved this by changing one line of client code.

I changed

var socket = io('localhost');

to

var socket = io();


来源:https://stackoverflow.com/questions/6736706/socket-io-access-control-allow-origin-error-from-remote-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!