前端代码 使用vue书写 端口号8081
<script>
var socket = io('http://192.168.XX.XXX:2121');
uid = 123;
// socket连接后以uid登录
socket.on('connect', function(){
//socket.emit('login', uid);
alert('链接成功');
});
// 后端推送来消息时
socket.on('new_msg', function(msg){
console.log("收到消息:"+msg);
});
</script>
Failed to load http://192.168.XX.XXX:2121/socket.io/?EIO=3&transport=polling&t=MxKjFKL: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.
是否是因为跨域问题
但是如果是跨域问题
使用了这两句话
仍然无效
header('Access-Control-Allow-Origin:*');//允许所有来源访问
header('Access-Control-Allow-Method:POST,GET');//允许访问的方式
请问解决了嘛