我正在尝试用我的前端调用我的API,但我有以下错误:
Access to XMLHttpRequest at '*' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
奇怪,因为根据文档,我只需要在我的app.py
中添加以下内容:
app = Flask(__name__)
app.config['CORS_HEADERS'] = "Content-Type"
app.config['CORS_RESOURCES'] = {r"*": {"origins": "*"}}
app.register_blueprint(node_blueprint, url_prefix='/api/v1/sample')
cors = CORS(app)
但是我已经blueprint
,所以在我的蓝图中我有:
@api.route('/', methods=["GET"])
@cross_origin()
def get:
...
@api.after_request
def set_cors_header(response):
response.headers['Access-Control-Allow-Origin'] = '*'
return response
你能解释一下吗?
不知道这是否会对您有所帮助,但我不久前遇到了类似的问题。我添加了以下内容:
在我的 CORS 中(因为我的 api 使用凭据(:
CORS(app, supports_credentials=True)
然后我的标题:
response.headers.add('Access-Control-Allow-Headers',
"Origin, X-Requested-With, Content-Type, Accept, x-auth")
response.headers.add('Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE')
希望这会有所帮助。