Forums

Flask CORS is not solving CORS issue

I've installed Flask CORS on my pythonanywhere account per flask-cors.readthedocs.io using

pips3.6 install --user flask-cors

I've loaded it in my app using:

from flask_cors import CORS
app = Flask(__name__)
CORS(app)

But nothing! When I load the page, I still get CORS errors. There is no indication in the pythonanywhere that any of the modules are missing or anything.

Reading the documentation for flask-cors I got the impression that once up and running, any CORS errors should be resolved for all pages in the site.

How can I get this to work?

probably app = CORS(app)

Changing it to app = CORS(app) makes the app fail. It goes from serving the data to throwing this error:

Error code: Unhandled Exception

I think Conrad was mistaken in his suggestion. Could you give an example of a page on your site that is generating the CORS errors so that we can take a look?

I have resolved the issue. The issue was that I was loading CORS but I also was setting the headers in the cross-origin headers in JSON object. Removed that and now it works!

Ah, excellent -- glad you worked it out!

Can you post the code for the solution? Im running into same issue. Thanks.

Can you post the code for the solution? Im running into same issue. Thanks.

This solved it for me:

from flask_cors import CORS   
app = Flask(__name__)
CORS(app)

And on the other side (react):

fetch("https://username.pythonanywhere.com/app", { method: 'get', mode: 'cors' }

Thanks!

Thanks! Can anyone teach me how to install flask_cors on pythonanywhere? Im new in the site. I tried from console but keep getting errors. Which would be the right sintaxis for that?

See http://help.pythonanywhere.com/pages/InstallingNewModules/