flask-cors: Getting CORS preflight error even with flask cors initialized
Hey guys, I’ve been trying to get flask_cors to work for over two weeks now, so thought that I would write an issue.
Here’s what is in the init.py file
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('config.py')
cors = CORS(resources={
r'/*': {
'origins': [
'http://localhost:8080'
]
}
})
cors.init_app(app)
The error that I’m getting is:
login?redirectNotebook=rose.intro:1 Access to XMLHttpRequest at 'http://localhost:5000/users' 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.
Went into the library to poke around and it looks like the after request isn’t being called. The backend is running on localhost:5000 and the frontend is running on localhost:8080, any help would be greatly appreciated!
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 5
- Comments: 28
Commits related to this issue
- fix: check before request CORS preflight channel did not succeed https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSPreflightDidNotSucceed Answer used https://github.com/corydolphin/f... — committed to t-h2o/matcha by t-h2o 2 years ago
- fix: check before request CORS preflight channel did not succeed https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSPreflightDidNotSucceed Answer used https://github.com/corydolphin/f... — committed to t-h2o/matcha by t-h2o 2 years ago
- fix: check before request CORS preflight channel did not succeed https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSPreflightDidNotSucceed Answer used https://github.com/corydolphin/f... — committed to t-h2o/matcha by t-h2o 2 years ago
@Elynad what I did is
This before request setup worked for me
Apparently AirPlay Receiver on Mac uses port 5000. It won’t block your server’s startup every time, but when it does Flask offers the helpful hint to disable it. However it seems to always interfere with CORS. Disable it in Settings > General > AirDrop & Handoff > AirPlay Receiver, or better yet, use another port.
@joaodlf
You can use this way. because it’s work for me
@app.before_request def before_request(): headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type' } if request.method == 'OPTIONS' or request.method == 'options': return jsonify(headers), 200it solve the cors issue
Wow. This works for me too. Effectively returning a 200 on OPTIONS requests (pre flight).
My app has no issues under 127.0.0.1 using this library, but as soon as I switched to localhost, I was hitting this same issue.
Would really like to understand why.
@ryanbrwr Does your OPTIONS call to server return 200 OK?
This worked brilliantly for me: https://github.com/corydolphin/flask-cors/issues/292#issuecomment-883929183
I gave up a long time ago and used a different lib, appreciate it though.
Sorry for adding a comment to this long open issue but hopefully this helps other people that end up here looking for a solution to their CORS issue.
I tried all of the above and nothing helped. I finally noticed an error in the browser’s console that said:
After some reading I realized I need to add this to my code:
Hopefully this helps out other people.
@shrhawk-entertainer It seems to be a good answer, but where do you get
requestfrom ?@cohi-dev Yes, I did
Here is a part of my code :
I also tried to replace
CORS(app)withcors = CORS(app, resources={r"/*": {"origins": "*"}})And I also tried to add
@cross_origin()under each@app.route([...])The thing is, everything is working fine in local (I am using dockers), but when these dockers are deployed on AWS EC2 instance, I get an XMLHttpRequest error 😕