What is the problem?
What is your code supposed to do?
It is supposed to send a login form (from http://127.0.0.1:5500/admin/login/index.html ) to my server (http://localhost:5000/), check if the credentials are correct, and, if they are, redirect user to the dashboard (http://127.0.0.1:5500/admin/dashboard/index.html ).
What is your code doing instead?
Throwing me a CORS error.
What inputs, if any, cause the problem?
when i fill out my form and send it, it gives me a cors error.
Is there an error message of some kind? If so, include it.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/admin/login/index.html. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
What have you tried?
What have you already tried to debug your own problem?
I tried to add the CORS Flask extension to my project but that did not work:
(init.py)
from flask import Flask
from flask_cors import CORS
from db import db, DB_NAME
app = Flask(__name__)
app.config["SECRET_KEY"] = "oitorent123"
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_NAME}"
db.init_app(app)
CORS(app=app)
from auth import auth
app.register_blueprint(auth, url_prefix="/admin")
if __name__ == "__main__":
app.run(debug=True, host="localhost", port=5000)
I tried enabling CORS to each page:
(init.py)
from flask import Flask
from flask_cors import CORS
from auth import dashboard, auth
from db import db, DB_NAME
app = Flask(__name__)
app.config["SECRET_KEY"] = "oitorent123"
app.config['CORS_HEADERS'] = 'Content-Type'
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_NAME}"
CORS(app, resources={r'/admin': {"origins": "http://127.0.0.1:5500"}})
CORS(auth, resources={r'/admin': {"origins": "http://127.0.0.1:5500"}})
CORS(dashboard, resources={r'/admin': {"origins": "http://127.0.0.1:5500"}})
db.init_app(app)
app.register_blueprint(auth, url_prefix="/admin")
app.register_blueprint(dashboard, url_prefix="/admin")
#db.create_all(app=app)
if __name__ == "__main__":
app.run(debug=True, host="localhost", port=5000)
I tried to add cross_origin()to each route:
(auth.py)
from flask import Flask, Blueprint, request, jsonify, session, json
from flask_cors import CORS, cross_origin
from werkzeug.utils import redirect
auth = Blueprint("auth", __name__)
dashboard = Blueprint("dashboard", __name__)
@auth.route("/login/index.html", methods=["GET", "POST"])
@cross_origin()
def login():
if request.method == "POST":
data = request.get_json()
username = data.get("username", "")
password = data.get("password", "")
isCredentialsCorrect = checkCredentials(username, password)
if (isCredentialsCorrect):
print('is correct')
session["is_logged_in"] = isCredentialsCorrect[0].get('loggedIn')
return redirect("/dashboard/index.html")
else:
session["is_logged_in"] = False
return redirect("/login/index.html")
What precisely are you confused by?
I do not understand why the CORS is still being blocked even if I have enabled it.
Have you tried googling for answers?
I tried these answers: How to enable CORS in python , How to enable CORS in flask , Solve Cross Origin Resource Sharing with Flask , https://dev.to/matheusguimaraes/fast-way-to-enable-cors-in-flask-servers-42p0 , https://www.arundhaj.com/blog/definitive-guide-to-solve-cors-access-control-allow-origin-python-flask.html , but none of them worked for me.