即使处理了 CORS,Python localhost 服务器也会在 http post 请求上返回 CORS 错误

Python localhost server return CORS error on http post request even if CORS is handled

提问人:Crocio 提问时间:11/17/2023 更新时间:11/17/2023 访问量:13

问:

我有这个尝试处理http POST请求的python代码(文件),我也有正确显示的文件(它只是一个进行http调用的按钮),我运行服务器,当我单击按钮时,我在控制台中收到此错误:test.pyindex.htmlpython test.py

访问在“http://127.0.0.1:8001/process_question”处获取 源“http://localhost:8001”已被 CORS 策略阻止:否 “Access-Control-Allow-Origin”标头存在于请求的 资源。如果不透明响应满足您的需求,请将请求的 mode 设置为“no-cors”,以在禁用 CORS 的情况下获取资源。

我以为我正确地处理了 CORS,但我想我不是,关于如何解决这个问题的任何建议?

from flask import Flask, request, jsonify, make_response, render_template
from flask_cors import CORS
import requests
import os
import PyPDF2
from transformers import pipeline

app = Flask(__name__)

api_cors_config = {
    "origins": ["*"],
    "methods": ["OPTIONS", "GET", "POST"]
}

CORS(app, resources={
    r"/*": api_cors_config
})

@app.route('/process_question', methods=['POST'])
def process_question():
    ...
    return ...

@app.route('/')
def home():
    return render_template('index.html')

if(__name__ == "__main__"):
    app.run(debug=True, port=8001)
python cors flask-cors

评论


答: 暂无答案