issue with flask-cors - blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status

☆樱花仙子☆ 提交于 2021-02-17 07:17:31

问题


I'm a newbie in flask and angular please bear with me.

I've been stuck with an issue about CORS. I've applied different code fixes just to make it work. Now the error that I am getting is

Access to XMLHttpRequest at 'http://localhost:5000/dashboard/clientscount/2019/2020' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I think the answer to my problem is from the answered question on this post: Has been blocked by CORS policy: Response to preflight request doesn’t pass access control check, specifcally this code

if r.Method == "OPTIONS" {
    w.WriteHeader(http.StatusOK)
    return
}

but this is in go language. I'm working in flask. my question is how do I make this in flask? also in the reference answer, it says to respond to the initial request but I'm not sure how to proceed with that one.

If you could point me in the right direction or docs, I'd gladly appreciate it.


回答1:


Hope this might help:

How to enable CORS in flask

You have to first install flask-cors by running: pip install -U flask-cors

Then import CORS and Cors-origin as follows:

from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route("/")
@cross_origin()


来源:https://stackoverflow.com/questions/61955973/issue-with-flask-cors-blocked-by-cors-policy-response-to-preflight-request-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!