1

如标题所述,情况如下:

烧瓶服务器

app = flask.Flask(__name__, static_folder='static')
SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
logger = logging.getLogger(__name__)
app.secret_key = 'v3563vt3vg3vf3rfgb4ty456b35453b54t3gfefert35'
CORS(app, resources={
    r"/*": {
        "origins": "*"
    }
}, supports_credentials=True)
socketio = SocketIO(app, engineio_logger=True, logger=True)

@app.route('/login', methods=['GET', 'POST'])
def login():
    json_data = request.get_json()
    session['email'] = json_data['email']
    session['name'] = json_data['name']
    return "authenticated"

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', 'http://localhost:8080')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    response.headers.add('Access-Control-Allow-Credentials', 'true')
    return response

@app.route('/checkDatabase', methods=['GET', 'POST'])
def check_database():
    to_check = os.path.join(os.getcwd(), "database_" + session['email'])
    to_send = ""
    if os.path.exists(to_check):
        to_send += "database found"
    else:
        to_send += "database NOT found"
    return to_send

Vue.js 前端

let form = JSON.stringify(this.form);
this.axios
    .create({ withCredentials: true })
    .post("http://localhost:6543/login", JSON.parse(form), {
      headers: {
      },
    })

从 POSTMAN 调用这两个 API,flask 可以成功存储它们的值,因为第二次调用返回所需的答案。相反,当在 localhost:8080 浏览器页面从 vuejs 调用它们时,我在主服务器上收到错误,因为它显示 KeyError 'email'。

我尝试了在网上找到的所有解决方案,但没有成功。我究竟做错了什么?

4

0 回答 0