我在生产的后端有一个带有 Flask 的 React 应用程序,我发现 React 没有到达我的端点。
我知道在使用客户端路由时,开发人员需要使用类似于以下的包罗万象的功能:
@app.errorhandler(404)
def error_handler(e):
return render_template('index.html')
我正在使用 Flask-CORS 来处理跨源请求:
在 config.py 中:
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 => { ... })
而且我没有收到任何错误。服务器的日志是干净的,控制台只显示Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com:5000/auth/login. (Reason: CORS request did not succeed)
. “原因:CORS 请求未成功”表示服务器没有返回任何内容。该应用程序渲染良好,但 React (axios) 无法到达我的端点。有什么我不知道的陷阱吗?发送请求时,我什至没有在网络选项卡中获得状态代码。
谢谢你。
网络选项卡的屏幕截图: