CORS problem (No 'Access-Control-Allow-Origin') on client
Problem
Hi all, Typically if you do have server with sockets on another domain and you do simple first line `io('YOUR_HOST')`, you will receive this message: `XMLHttpRequest cannot load YOUR_HOSTsocket.io/?EIO=3&transport=polling&t=1446467052356-0. No 'Access-Control-Allow-Origin'...` First appeared in head, that I need to look at documentation and find out how to set it from there, but eventually I did not find anything because of pure documentation. Then I entered source code and the only 2 usages of `setRequestHeader` method, which is actually allow you to do that, is only for setting content-type if POST method is chosen. Could you help me to fix this issue?
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: CORS problem (No 'Access-Control-Allow-Origin') on client
I am a bit confused by your code. The allowed origins option goes in your socket.io server not in the client i.e. in node.js code not in browser javascript that is part of a html page. Also I usually use a wildcard on the port and you can specify multiple allowed origins, just space them out like so: [code block] On the client side [code block]
Trust Score
3 verifications
- 1
I am a bit confused by your code. The allowed origins option goes in your socket
I am a bit confused by your code. The allowed origins option goes in your socket.io server not in the client i.e. in node.js code not in browser javascript that is part of a html page. Also I usually use a wildcard on the port and you can specify multiple allowed origins, just space them out like so:
text//in node.js var app = require('express')(); var io = require('socket.io'); var server = require('http').createServer(app); var allowedOrigins = "http://localhost:* http://127.0.0.1:*"; var path ='/stomp'; // you need this if you want to connect to something other than the default socket.io path var sio_server = io(server, { origins: allowedOrigins, path : path }); ``` On the client side ``` var clientSocket = io({path:'/stomp'});
Validation
Resolved in socketio/socket.io GitHub issue #2294. Community reactions: 3 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep