Ionic应用错误:请求的资源上不存在’Access-Control-Allow-Origin’标头。

huangapple go评论103阅读模式
英文:

Ionic app error: No 'Access-Control-Allow-Origin' header is present on the requested resource

问题

我有一个使用Ionic Capacitor开发的移动应用程序。该应用程序的后端是部署在Amazon Elastic Beanstalk上的.NET Core Web API。当尝试使用该应用程序访问后端时,我收到了CORS错误未在请求的资源上出现'Access-Control-Allow-Origin'标头

我尝试允许任何消费者访问API,允许所有来源。是否需要在AWS Elastic Beanstalk上配置任何内容?

var app = builder.Build();
app.UseCors(builder => builder
     .AllowAnyOrigin()
     .AllowAnyMethod()
     .AllowAnyHeader()
     );
英文:

I have a mobile app developed in ionic capacitor. The backend to the app is a .net core web api deployed on amazon elastic beanstalk. I am getting CORS error** No 'Access-Control-Allow-Origin' header is present on the requested resource** when trying to access the back end using the app.

I have attempted to allow the API to be accessible by any consumer by allowing all origins. Is there need to configure anything on AWS Elastic bean?

var app = builder.Build();
app.UseCors(builder => builder
     .AllowAnyOrigin()
     .AllowAnyMethod()
     .AllowAnyHeader()
     );

答案1

得分: 0

  1. 登录到AWS管理控制台,然后导航到弹性Beanstalk服务。
  2. 选择您的应用程序和部署了.NET Core Web API的环境。
  3. 在导航窗格中,点击“配置”。
  4. 在“软件”部分下,点击“编辑”以修改“环境属性”。
  5. 添加一个新的属性,具体如下:
    • 名称:ACCESS_CONTROL_ALLOW_ORIGIN
    • 值:*(或者如果不想允许所有来源,可以指定特定的来源)
  6. 保存更改,等待环境更新。

确保从您的.NET Core Web API代码中删除您提到的CORS配置,因为现在CORS处理将由弹性Beanstalk处理。

英文:
  1. Log in to the AWS Management Console and navigate to the Elastic Beanstalk service.
  2. Select your application and environment where the .NET Core Web API is deployed.
  3. In the navigation pane, click on "Configuration."
  4. Under the "Software" section, click on "Edit" for the "Environment properties."
  5. Add a new property with the following details:
    • Name: ACCESS_CONTROL_ALLOW_ORIGIN
    • Value: * (or the specific origin you want to allow if you don't want to allow all origins)
  6. Save the changes and wait for the environment to update.

Make sure to remove the CORS configuration you mentioned from your .NET Core Web API code, as the CORS handling will now be done by Elastic Beanstalk.

huangapple
  • 本文由 发表于 2023年6月1日 21:53:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定