Menu Close

How to solve cross origin resource sharing error with Flask and Python?

Sometimes, we want to solve cross origin resource sharing error with Flask and Python.

In this article, we’ll look at how to solve cross origin resource sharing error with Flask and Python.

How to solve cross origin resource sharing error with Flask and Python?

To solve cross origin resource sharing error with Flask and Python, we can use the flask-cors package.

To install it, we run

pip install -U flask-cors

Then we use it by writing

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route("/")
def hello_world():
   return "Hello, cross-origin-world!"

to call CORS with app to enable CORS throughout the app.

Conclusion

To solve cross origin resource sharing error with Flask and Python, we can use the flask-cors package.

Posted in Python, Python Answers