Forums

CORS Error when requesting my website.

I have set up an API with Flask on my account and I am accessing it from Heroku. Most of the times the website refuses connection, but this never happens from my machine, so I think this is a CORS problem.

I am passing a user agent and a referer in the headers of the request but it's not working sometimes.

What can I do to avoid this?

What is the exact error you are getting? How is your API configured? How does the client code look like?

I kinda fixed it by using flask_cors.

This is from SO:

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, cross-origin-world!"

This fixed the error on Heroku, but I don't know if it will be consistent.

Ok. Glad you got it working.

I am getting the below error:

Access to XMLHttpRequest at 'https://---------------.pythonanywhere.com/' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

What is the context of that error? What url? What code? If you do not want to share it on public forums, email us at support@pythonanywhere.com