【问题标题】:NodeJs express.io Cross-Origin Request BlockedNodeJs express.io 跨域请求被阻止
【发布时间】:2015-07-14 19:02:16
【问题描述】:

我是 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>

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    您遇到了 CORS 问题。鉴于从域 A 加载的页面,出于安全原因,未经域 B 的同意,您不能向域 B 发出请求。请参阅http://acuriousanimal.com/blog/2011/01/27/working-with-the-javascript-xmlhttprequest-object/ 以获得更好的解释。

    我没用过 express.io 框架,但它可能是基于 XMLHttpRequest 进行请求调用。

    您必须启用cors 模块(请参阅https://github.com/techpines/express.io/issues/117)或相同的,将Access-Control-Allow-* 附加到服务器的响应中。

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 2020-12-22
      • 2021-06-26
      • 2014-10-13
      • 2019-11-04
      • 2017-02-06
      • 2017-01-04
      相关资源
      最近更新 更多