1

我正在尝试允许对我的 Flask 应用程序的同源请求:

这是在我的__init__.py文件中:

# ... other imports ...
from flask_cors import CORS

cors = CORS()


def create_app(script_info=None):
    app = Flask(__name__)
    app_settings = os.getenv('APP_SETTINGS')
    app.config.from_object(app_settings)

    from project.api.flakes import flakes_blueprint
    app.register_blueprint(flakes_blueprint)
    cors.init_app(flakes_blueprint, resources={r"/flakes": {"origins": "*"}})

    return app

根据文档,这应该足以让它工作,但我得到:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5001/flakes. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)

我怎样才能让它工作?我会认为{"origins": "*"}涵盖了一切。

4

1 回答 1

0

我没有看到像您提供的示例 using cors_init_app(),因此无法评论为什么它不起作用。

但是,这是我以前使用过的一个例子:

app = Flask(__name__)
CORS(app, resources={r'/*': {'origins': '*'}})
app.config['CORS_HEADERS'] = 'Content-Type'
app.register_blueprint(my_blueprint_module.bp)
于 2020-07-26T20:01:43.087 回答