angular - Angular中的CORS错误-基于flask socketIO
问题描述
我正在开发一个基于 Angular 的应用程序通过 Socket.IO 与 Flask 通信的项目。我的烧瓶代码如下:
from flask import Flask, render_template
from flask_socketio import SocketIO
from flask_cors import CORS
import json
app = Flask(__name__)
CORS(app)
#app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app)
@app.route('/')
def sessions():
return render_template('index.html')
def messageReceived(methods=['GET', 'POST']):
print('message was received!!!')
@socketio.on('my event')
def handle_my_custom_event(json, methods=['GET', 'POST']):
json["user_name"]='Asad'
json["user_input"]='My Server'
print('received my event: ' + str(json))
socketio.emit('my response', json, callback=messageReceived)
if __name__ == '__main__':
socketio.run(app, debug=True, host='localhost', port = 5000)
当我尝试加载我的 Angular 项目时,它立即显示与 CORS 相关的错误。我已经尝试安装 Moesif Origin & CORS Changer 并将 CORS 包含到服务器代码中,可以看出但没有任何改进。Angular 代码取自 [https://tutorialedge.net/typescript/angular/angular-socket-io-tutorial/][1]
但是,我没有使用该教程中显示的 websocket 服务器,因为正在使用 Flask 端的服务器:
显示的错误是:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/socket.io/?EIO=3&transport=polling&t=NCws9C7。(原因:CORS 请求未成功)。[1]:https ://tutorialedge.net/typescript/angular/angular-socket-io-tutorial/
解决方案
推荐阅读
- json - requests.responce.json() 函数给出错误“期望值:第 1 行第 1 列(字符 0)”
- javascript - 超过最大调用堆栈大小 select2
- javafx - 将图标设置为 JFXButton
- java - hashmap.get() 的拆箱正在产生 NullPointerException
- c# - 远程连接断开时如何在远程服务器中模拟鼠标单击或移动?
- javascript - Webpack:无效的配置对象/无效的模块条目
- java - 如何避免空指针异常使我的应用程序崩溃?
- javascript - 如何将事件对象传递给嵌套函数?
- javascript - ReactJS 导出到 Excel 工作表
- docker - 在 Kubernetes 集群中部署 Jenkins