Forums

Flask and Elm - CORS rejection

I've just implemented a simple Flask server. I am using Elm as my front end to request JSON. The JSON displays fine when I open my site in my browser, however requesting my elm app, the javascript console says:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Is there a simple fix for this?

that doesn't sound like a pythonanywhere-specific problem. have you tried a general google for it?

I just fixed it using Flask-CORS. Also I was returning my JSON as a JSON array which may have added to my problem. Thanks for the response!

glad you figured it out! :)

i am calling my rest api from a anguler6 app, and it shows me a a "No 'Access-Control-Allow-Origin' header is present" problem. i tried to use "flask_cors" but, if i use "flask_cors" like this ---

from flask_cors import CORS; app = Flask(name); CORS(app);

my api in pythonanywhere just crushed.

what do you mean by your api is crushed? does your whole app fail with 502s? or just your api requests return 403? what are the error messages and stack traces?

whole app fail. Error code: Unhandled Exception

Look in your error and server logs to find tracebacks that you can use to start debugging your web app.

error log said, "FileNotFoundError: [Errno 2] No such file or directory: '/home/mahadyHasan/mysite/pkl_objects/stopwords.pkl' " but my pickles are in the folder "pkl_objects". i use this line of code to load pickle file. "stop = pickle.load(open(os.path.join(cur_dir, 'pkl_objects', 'stopwords.pkl'), 'rb'))" if i don't use "from flask_cors import CORS " , everything works perfectly

I think you're looking at the wrong error message -- the most recent errors are at the bottom of the file. If you look there you'll see an error importing flask_cors. The Flask-Cors package is not installed by default, but you can install it into your account using the techniques on this help page.

Also -- don't forget that you need to reload your website on the "Web" page after making any code changes -- which includes after installing Python packages like Flask-Cors.

thank you so much. i don't know that flask_cors is not installed by default.not it works fine.

Excellent, glad it's working now.

You can see a full list of pre-installed packages here.