gpt4 book ai didi

amazon-web-services - AWS Elastic beanstalk 上的 CORS

转载 作者:行者123 更新时间:2023-12-03 17:03:38 25 4
gpt4 key购买 nike

我是 AWS 的新手并且使用过

  • 弹力 bean 茎在节点
  • 中部署我的 rest API (api.example.com)
  • S3 存储桶 云前线我在 React 中的静态网站(example.com)。


  • 从网站调用 API 端点时,浏览器给出 CORS 错误。我怎样才能防止这种情况?

    我在 CORS 的节点项目中使用以下代码
    app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Credentials', true)
    res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
    next();
    });

    编辑 - 基于阿鲁津斯卡的评论

    我还在bucket中配置了CORS
    <?xml version="1.0" encoding="UTF-8"?>
    <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    </CORSRule>
    </CORSConfiguration>

    and the server is on nginx



    在此先感谢您的帮助

    P.S - also, i have seen that few posts gives the reason as there would be some bug in the project code but all the endpoints are working correctly on POSTMAN.

    最佳答案

    我已将 AWS 中的 CORS 配置更新为

    <?xml version="1.0" encoding="UTF-8"?>
    <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
    <AllowedOrigin>http://localhost:3000</AllowedOrigin>
    <AllowedOrigin>https://example.com</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
    </CORSRule>
    </CORSConfiguration>

    关于amazon-web-services - AWS Elastic beanstalk 上的 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53371763/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com