简体   繁体   English

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

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

Background: I have the frontend (Vue) of my app deployed on AWS Amplify and the Backend (Python) on AWS Elastic Beanstalk.背景:我的应用程序的前端 (Vue) 部署在 AWS Amplify 上,后端 (Python) 部署在 AWS Elastic Beanstalk 上。 My frontend is connected to the backend via Axios.我的前端通过 Axios 连接到后端。

Problem: When sending data from the front end to the backend, I get a 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.问题:从前端向后端发送数据时,出现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.

When my frontend is sending data to the localhost, there's no issue, so I don't think it's an issue with my code.当我的前端向本地主机发送数据时,没有问题,所以我认为这不是我的代码的问题。 Is there something about the Elastic Beanstalk configuration I need to change?是否需要更改有关 Elastic Beanstalk 配置的某些内容?

I'm not sure what all code I need to include to get a proper answer, so please let me know what you'd like to see and I can update the question appropriately.我不确定我需要包含哪些代码才能得到正确的答案,所以请告诉我你想看到什么,我可以适当地更新问题。 I've added my application.py code below and am using Flask-CORS currently.我在下面添加了我的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()

Your configuration looks fine.你的配置看起来不错。 Please look for errors in the elasticbeantalk logs.请在 elasticbeantalk 日志中查找错误。 It is possible that the application throws some errors in the elasticbeanstalk environment, during which the CORS headers are not returned.应用程序可能会在 elasticbeanstalk 环境中抛出一些错误,在此期间不会返回 CORS 标头。

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

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