node.js - nodejs使用socket.io时出现的问题,具体见描述

查看:110
本文介绍了node.js - nodejs使用socket.io时出现的问题,具体见描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

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.

求大神解惑,万分感谢!

解决方案

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

这篇关于node.js - nodejs使用socket.io时出现的问题,具体见描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆