angularjs - 套接字 io 的奇怪 cors 问题

标签 angularjs node.js sockets express cors

我正在使用 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/

相关文章:

python - 从 SocketServer.ForkingTCPServer python 释放套接字

javascript - Angularjs $http 不会在 GET 请求中调用 api

javascript - 使用 Angular 发布到 APi

javascript - 使用 angularjs animate 动画 div

c - C 中用于网络编程的 accept() 函数

python - 启动 HTTP/HTTPS 服务器,python -m SimpleHTTPServer

angularjs - Angular ng-if 抛出 TypeError : Cannot read property '0' of null

node.js - Openshift平台上的Node.js Oracle Db连接

javascript - 异步等待函数

javascript - 如何解决NodeJS方法优先级问题