问题描述
我在生产后端有一个带有 Flask 的 React 应用程序,我发现 我的端点都没有从 React 到达。
我知道在使用客户端路由时,开发人员需要使用包罗万象的功能 类似于以下内容:
"query": { "city":"London","active_delivery_categories": { "$in" : ["biker"]},"ban_status": False}
我正在使用 Flask-CORS 来处理跨源请求:
在 config.py 中:
@app.errorhandler(404)
def error_handler(e):
return render_template('index.html')
我的蓝图:
class ProductionConfig:
CORS_HEADERS = 'Content-Type'
...
...
在前端,我定义了这样的标题:
CORS(auth_blueprint,resources={r'/*': {"origins": "https://example.com","allow_headers": "*","expose_headers": "*"}})
@auth_blueprint.route('/auth/login',methods=['POST'])
@cross_origin()
def login():
...
...
而且我没有收到任何错误。服务器的日志是干净的,控制台只显示 const headers = { "Access-Control-Allow-Origin": "*" };
const url = "https://example.com:5000/auth/login";
axios.post(url,data,headers).then(resp => { ... })
。
“原因:CORS 请求没有成功”表示服务器没有返回任何内容。
该应用程序呈现良好,但 React (axios) 无法到达我的端点。有什么我不知道的问题吗?发送请求时,我什至没有在网络选项卡中收到状态代码。
谢谢。
网络标签的屏幕截图:
解决方法
您需要将连接协议更改为 http
。