我一直在浏览器上遇到此 CORS 问题:“从源 'http://localhost:3000' 获取访问 'http://localhost:5000/register' 已被 CORS 策略阻止:对预检请求的响应没有通过访问控制检查:它没有 HTTP ok 状态。”我尝试了使用flask_cors、设置标头以在检测到OPTIONS 请求时允许CORS 等所有可能的技术。但是,我仍然遇到相同的错误。你知道我该如何解决这个问题吗?
init.py
from flask import Flask
from flask_socketio import SocketIO
from flask_socketio import join_room, leave_room, send
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_jwt_extended import JWTManager
from flask_cors import CORS
app = Flask(__name__)
app.config['SECRET_KEY'] = "\xe1\xe1\xf1%S!\xe7\xb6J%\xb5\x14\xc1#\x1a\x05\x89\x7f\xb0\x9e\x0e\x07\xb1\t"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
app.config['UPLOAD_FOLDER'] = r"C:\Users\matth\Personal_Projects\social-media-app\public\images\userUploads"
app.config['CORS_HEADERS'] = 'Content-Type'
CORS(app, origins = ['http://localhost:3000'])
bcrypt = Bcrypt(app)
socketio = SocketIO(app, cors_allowed_origins = "*")
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
jwt = JWTManager(app)
routes.py
from flask_socketio import join_room, leave_room, send
from werkzeug import secure_filename
from database.UserModel import User
from database import app, db
from flask import request, make_response
from flask_login import login_user
import json
import bcrypt
from werkzeug.utils import secure_filename
import os
from flask_jwt_extended import create_access_token
from flask_jwt_extended import get_jwt_identity
from flask_jwt_extended import set_access_cookies
from flask_jwt_extended import unset_jwt_cookies
from flask_jwt_extended import jwt_required
from flask_cors import cross_origin
@app.route("/register", methods= ['POST', 'GET', 'OPTIONS'])
@cross_origin()
def registerForm():
if request.method == 'POST':
print("tested", flush=True)
userName = request.form["userName"]
email = request.form["Email"]
password = request.form["password"]
confirmPassword = request.form["confirmPassword"]
duplicateEmail = User.query.filter_by(email=email).first()
duplicateUser = User.query.filter_by(userName = userName).first()
print("sent by server", flush=True)
file = request.files["file"]
if file:
filename = secure_filename(file.filename + "-" + userName)
print("File name is " + filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if password != confirmPassword:
return json.dumps({"passwordError": "Passwords Do Not Match! "}), 403
if duplicateEmail is not None:
return json.dumps({"duplicateEmailError": "E-mail Already Exists! Please sign in."}), 409
if duplicateUser is not None:
return json.dumps({"duplicateUserNameEror": "Username Already Exists! Please choose a different one."}), 409
pw_hash = bcrypt.generate_password_hash(password).decode('utf-8')
user = User(userName = userName, email = email, password = pw_hash, image_url = filename, online = False )
db.session.add(user)
db.session.commit()
return json.dumps({"success": "ok"}), 200
if request.method == 'OPTIONS':
print("Hello", flush= True)
res = make_response()
res.headers.add('Access-Control-Allow-Origin', "*")
res.headers.add('Access-Control-Allow-Methods', "*")
res.headers.add("Access-Control-Allow-Headers", [" Content-Type", "Access-Control-Allow-Origin"])
return res