Forums

Flask-CORS Error

Hello, I have this CORS implementation in my code:

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

The error I'm getting is:

Access to XMLHttpRequest at 'https://user.pythonanywhere.com/' from origin 'http://www.mysite.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Please help. Don't know what I might be missing and where. Thank you.

Could you give some more details on what you're trying to get working? That message on its own is just a sign of the CORS module doing what it's meant to do.

According to the CORS documentation, doing this:

CORS(app)

Will allow communication to my app from another sites. When I try to do so, I get the error mentioned.

What I'm trying to do is have another website use the webapp I have on pythonanywhere by enabling the CORS module. Every time I attempt to communicate with https://user.pythonanywhere.com/, its unsuccessful. The error above is received.

That would appear to be all that is necessary. Have you reloaded the web app since you added the CORS module? Have you checked the response that you're getting in the browser to see whether it's an error or something like that?

I was able to fix the error. This is how the code was set when it worked:

from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})

I had to explicitly authorize flow from any website.

Weird. I thought that was the default that the CORS package provided.

How to install flask cors in my environment?

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