【问题标题】:Socket.IO CORS request failedSocket.IO CORS 请求失败
【发布时间】:2015-05-29 03:01:43
【问题描述】:

我在客户端有一个角度模块:

angular.module('App')
  .factory('socketClient', function(socketFactory, Auth) {

    // socket.io now auto-configures its connection when we ommit a connection url
    var ioSocket = io('', {
      // Send auth token on connection
      query: 'token=' + Auth.getToken(),
      path:  '/socket.io-client'
    });

    return socketFactory({
    ioSocket: ioSocket
  });;
  });

如果我想请求其他命名空间,我会进行下一个更改:

    var ioSocket = io('/test', {
      // Send auth token on connection
      query: 'token=' + Auth.getToken(),
      path:  '/socket.io-client'
    }

但是当我这样做时,我会在浏览器中获得以下登录信息。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/socket.io-client/?token=&EIO=3&transport=polling&t=1432867748704-8. (Reason: CORS request failed).

我只是在连接到命名空间时收到此错误。

对不起我的英语。 c:

【问题讨论】:

标签: angularjs node.js firefox express socket.io


【解决方案1】:

我通过以下方式解决了这个问题:

angular.module('App')
.factory('socketClient', function(socketFactory, $window) {

  // with the full path: $window.location.origin + '/test'
  var ioSocket = io($window.location.origin + '/test', {
    // Send auth token on connection
    query: 'token=' + Auth.getToken(),
    path:  '/socket.io-client'
  });

  return socketFactory({
    ioSocket: ioSocket
  });
});

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 2017-05-20
    • 2013-12-06
    • 2019-07-17
    • 2017-02-14
    • 2021-11-19
    • 2014-11-23
    • 2014-02-14
    相关资源
    最近更新 更多