Javascript 连接Flask socketio应用程序时出错

Javascript 连接Flask socketio应用程序时出错,javascript,python,heroku,flask,socket.io,Javascript,Python,Heroku,Flask,Socket.io,我有一个Flask socket.io应用程序(后端)托管在Heroku上 我有一个带有JS代码的前端,可以连接到本地机器上的主机。 尝试连接到服务器时出现以下错误 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://deploy-appp.herokuapp.com/socket.io/? EIO=3&transport=pollin

我有一个Flask socket.io应用程序(后端)托管在Heroku上

我有一个带有JS代码的前端,可以连接到本地机器上的主机。 尝试连接到服务器时出现以下错误

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://deploy-appp.herokuapp.com/socket.io/?

EIO=3&transport=polling&t=MH05dP-. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://deploy-appp.herokuapp.com/socket.io/?

EIO=3&transport=polling&t=MH05f5A. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

JS套接字连接代码:

<script type="text/javascript">
var socket = io.connect('https://deploy-appp.herokuapp.com/');
socket.on('connect', function() {
console.log('connected');
});
socket.on('message', function(msg) {
document.getElementById('messages').append('<li>'+msg+'</li>');
console.log('Received message');
});
function send() {

console.log(document.getElementById('myMessage').value)
socket.send(document.getElementById('myMessage').value)

};
</script>

var socket=io.connect('https://deploy-appp.herokuapp.com/');
socket.on('connect',function(){
console.log('connected');
});
socket.on('message',函数(msg){
document.getElementById('messages').append('
  • '+msg+'
  • '); console.log(“收到的消息”); }); 函数send(){ console.log(document.getElementById('myMessage').value) socket.send(document.getElementById('myMessage').value) };
    查看。通常,如果前端应用程序正在从不同的uri“运行”到后端,则会出现类似这样的错误。例如,客户端代码的主机是myclient.com,但是socket.io后端正在另一个client.com上运行

    对此有很多解决方案,您可以显式设置原点,请参见此答案。(如果您在本地运行web应用程序,这是最快的事情,但不是长期安全的)

    或者,在客户端web服务器和socketio应用程序前面安装一个反向代理,该代理将客户端/socketio调用路由到socketio服务器,另一个请求路由到承载站点的服务器