English 中文(简体)
页: 1 IO - 不能在谷歌 Chrome装XMLHttpRequest
原标题:Socket.IO - cannot load XMLHttpRequest in Google Chrome

我试图从袖珍中找到第一个例子。 io(19.0.1048.46),但我发现错误:

“XMLHttpRequest不能装上http:// localhost:8080/socket.io/1/?t=1337156 198176。 出入控制-Allow-Origin不允许原产地无效。

服务器代码:

var app = require( http ).createServer(handler)
  , io = require( socket.io ).listen(app)
  , fs = require( fs )

app.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname +  /index.html ,
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end( Error loading index.html );
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on( connection , function (socket) {
  socket.emit( news , { hello:  world  });
  socket.on( my other event , function (data) {
    console.log(data);
  });
});

客户代码:

<script src="socket.io/socket.io.js"></script>
<script>
  var socket = io.connect( http://localhost:8080 );
  socket.on( news , function (data) {
    console.log(data);
    socket.emit( my other event , { my:  data  });    
  });
</script>

该法典有什么错误?

问题回答

我认为,你在浏览器中直接打开index.html文档,是这种情况吗?

<file: protocol no origin is set to the XMLHttpRequest, so the browser will rise that security exception if theuser don t setAccess-Control-Allow-Origin 1. 有利于CORS的负责人

avigate to http:// localhost:8080/, otherwise, thehandler specified in the Code should make the index.html page disabilities





相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签