NodeJs express.io跨源请求被阻止

时间:2015-02-26 19:12:13

标签: node.js express

我是nodejs和express.io的新手,我正在尝试使用express.io示例来测试广播,但是我得到了这个错误,我无法修复它

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://file/socket.io/?EIO=3&transport=polling&t=1424977776399-0. This can be fixed by moving the resource to the same domain or enabling CORS.

示例代码:

app = require('express.io')()
app.http().io()

// Broadcast the new visitor event on ready route.
app.io.route('ready', function(req) {
    req.io.broadcast('new visitor')
})

// Send client html.
app.get('/', function(req, res) {
    res.sendfile(__dirname + '/client.html')
})

app.listen(7076) 

HTML:

<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
<script src="socket.io.js"></script>
<script>

io = io.connect()

// Send the ready event.
io.emit('ready')

// Listen for the new visitor event.
io.on('new visitor', function() {
    $('body').append('<p>New visitor, hooray! ' + new Date().toString() +'</p>')
})
</script>

0 个答案:

没有答案