我正在使用 btford.socket-io,并尝试与 Node 通信。
它工作正常,但在我从 localhost:5000 更改为 example.org 后就不行了,如下所示:
.factory('socket',function(socketFactory){
//working
var myIoSocket = io.connect('http://localhost:5000');
//not working
var myIoSocket = io.connect('http://example.org:5000');
mySocket = socketFactory({
ioSocket: myIoSocket
});
return mySocket;
})
我的控制台出现此错误
XMLHttpRequest cannot load http://example.com:5000/socket.io/?EIO=3&transport=polling&t=LIGH5VO. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:8100' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.
:8100/#/app/completed:1 XMLHttpRequest cannot load http://example.com:5000/socket.io/?EIO=3&transport=polling&t=LIGH7BS. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:8100' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.
我还尝试使用 this 设置 cors({origin:"*"})
npm 在我的 express 中,但没有运气。
最佳答案
这是关于这个当凭据标志为 true 时,不能在“Access-Control-Allow-Origin” header 中使用通配符“*”。
设置允许来源为固定域将解决此问题,目前,如果我没有记错的话,http://example.org:5000
。我相信您应该包含端口号。
我不太清楚为什么你要打开 header 凭据。
关于angularjs - 套接字 io 的奇怪 cors 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099721/