Sometimes, we want to fix Cross Origin Resource Sharing issues with Python Flask
In this article, we’ll look at how to fix Cross Origin Resource Sharing issues with Python Flask
How to fix Cross Origin Resource Sharing issues with Python Flask?
To fix Cross Origin Resource Sharing issues with Python Flask, we can use the CORS
class.
For instance, w write
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()
def helloWorld():
return "Hello"
to create an app with CORS enabled.
We enable CORS on the app with cors = CORS(app)
.
And then we enable CORS our view function with the @cross_origin()
decorator.
Conclusion
To fix Cross Origin Resource Sharing issues with Python Flask, we can use the CORS
class.