Flask服务器的400 Bad Request错误反应为socketio的客户端 [英] 400 Bad Request error with flask server react client for socketio

查看:542
本文介绍了Flask服务器的400 Bad Request错误反应为socketio的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在flask服务器和reactjs客户端之间创建一个socketio链接.它显示此错误

Trying to create a socketio link between flask server and reactjs client. It shows this error

通过' http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=MrcruFC '来自来源'

"Access to XMLHttpRequest at 'http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=MrcruFC' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

我尝试从flask cors文档中包括CORS,但仍然无法正常工作.

I have tried including CORS from the flask cors documentation but it still doesnot work.

服务器:

from flask import Flask, Response
from flask_cors import CORS
from flask_socketio import SocketIO

app = Flask(__name__)
cors = CORS(app)
socketio=SocketIO(app)


@socketio.on('connection')
def handle_my_custom_event():
    socket.emit('outgoing data',{num: '10'})

@app.route("/")
def hello():
    return 'Hello'

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000)

推荐答案

您可以添加用于创建SocketIO的选项.

You can add an option for creating SocketIO.

socketio = SocketIO(app=app, cors_allowed_origins='*')

这篇关于Flask服务器的400 Bad Request错误反应为socketio的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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