Flask 服务器的 400 Bad Request 错误为 socketio 反应客户端
400 Bad Request error with flask server react client for socketio
正在尝试在 flask 服务器和 reactjs 客户端之间创建 socketio link。它显示此错误
"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,但它仍然不起作用。
服务器:
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)
您可以使用以下 headers 允许 CORS:
header = response.headers
header['Access-Control-Allow-Origin'] = '*'
您可以添加用于创建 SocketIO 的选项。
socketio = SocketIO(app=app, cors_allowed_origins='*')
正在尝试在 flask 服务器和 reactjs 客户端之间创建 socketio link。它显示此错误
"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,但它仍然不起作用。
服务器:
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)
您可以使用以下 headers 允许 CORS:
header = response.headers
header['Access-Control-Allow-Origin'] = '*'
您可以添加用于创建 SocketIO 的选项。
socketio = SocketIO(app=app, cors_allowed_origins='*')