繁体   English   中英

将 AWS Amplify 前端连接到 AWS Elastic Beanstalk 后端时的 CORS 问题

[英]CORS issue when connecting AWS Amplify frontend to AWS Elastic Beanstalk backend

背景:我的应用程序的前端 (Vue) 部署在 AWS Amplify 上,后端 (Python) 部署在 AWS Elastic Beanstalk 上。 我的前端通过 Axios 连接到后端。

问题:从前端向后端发送数据时,出现CORS error Access to XMLHttpRequest at '***' from origin '***' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

当我的前端向本地主机发送数据时,没有问题,所以我认为这不是我的代码的问题。 是否需要更改有关 Elastic Beanstalk 配置的某些内容?

我不确定我需要包含哪些代码才能得到正确的答案,所以请告诉我你想看到什么,我可以适当地更新问题。 我在下面添加了我的application.py代码,目前正在使用 Flask-CORS。

from flask import Flask, render_template
from flask import request, session
from flask_cors import CORS
import os
from common.api import api
from journal_blueprint import journal_blueprint
from manuscript_blueprint import manuscript_blueprint
from user_blueprint import user_blueprint
from models.decorators import requires_login
from models.user import User
from models.manuscript import Manuscript
from models.journal import Journal
from config_file import BaseConfig

application=Flask(__name__)

application.secret_key = BaseConfig.SECRET_KEY
application.config['SQLALCHEMY_DATABASE_URI'] = os.environ["DATABASE_URL"]

CORS(application)

application.register_blueprint(journal_blueprint, url_prefix='/journal')
application.register_blueprint(user_blueprint, url_prefix='/user')
application.register_blueprint(manuscript_blueprint, url_prefix='/manuscript')
application.register_blueprint(api, url_prefix="/api")


@application.route('/')
def home_template():
    return render_template('index.html')


@application.route('/login')
def login_template():
    return render_template('user/login.html')


@application.route('/register')
def register_template():
    return render_template('user/register.html')


if __name__ == '__main__':
    # application.run(debug=True)
    application.run()

你的配置看起来不错。 请在 elasticbeantalk 日志中查找错误。 应用程序可能会在 elasticbeanstalk 环境中抛出一些错误,在此期间不会返回 CORS 标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM