简体   繁体   中英

Socket.IO CORS request failed

I have my in the client side an angular module:

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
  });;
  });

if I want request for other namespace, I do the next change:

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

But when i do this, I get the follow log in browser.

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

I get this error just when I connect to the namespace.

Sorry for my English. c:

I solved this by:

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
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM