搜索
node.js - nodejs使用socket.io时出现的问题,具体见描述
阿神
阿神 2017-04-17 13:41:21
[Node.js讨论组]

为了解决跨域访问问题,我在后台设置了如下代码:

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

然后在使用socket.io时,浏览器控制台报以下错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 404.

求大神解惑,万分感谢!

阿神
阿神

闭关修行中......

全部回复(3)
黄舟

原因已找到,因为后台设置的webserver监听端口和socket.io监听端口一致

黄舟

你试试用app.use,socket的请求好像不走route,app.all是route里面的

app.use(function (req, res, next) {
    //...
});
伊谢尔伦

处理跨域直接使用cors package,不需要自己再去实现了。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号