chip
chip

Reputation: 3279

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

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.

Upvotes: 4

Views: 20596

Answers (3)

Michael Eliot
Michael Eliot

Reputation: 1176

For anyone using blueprint. You have to wrap blueprint in cors too.

blueprint = Blueprint("foo", __name__, url_prefix="/api/bar")
CORS(blueprint)

Upvotes: 3

rodrigolmti
rodrigolmti

Reputation: 111

As the comments on the link describes, if you have setup CORS in your application and still has the problem, possible is because your application is calling a path that may differ from the path that you register.

example:

Flask url: http://localhost:3000/api/v1/support-user/

Called url: http://localhost:3000/api/v1/support-user

Missing / at the end

You can ensure that the problem is this if you see in your logs that flask redirect the request with 308

https://github.com/corydolphin/flask-cors/issues/257

Upvotes: 10

devqik
devqik

Reputation: 41

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()

Upvotes: 0

Related Questions