如何解决浏览器扩展无法解决的 CORS 错误?
How to solve a CORS error that cannot be solved with a browser extension?
我有一个工作的 React 应用程序,它的工作后端是用 .NET Core 制作的。我遇到了 CORS 问题。清楚记录的错误。最初,我通过使用 chrome 扩展名 (this one) as explained here.
“解决”了这个问题
后来,我尝试了一个更好的选择,allowing CORS via code in my .NET Core application。这解决了我的问题。我的 React 应用程序(使用 localhost:3000)能够使用后端 (localhost:5000)。不再需要扩展。
从逻辑上讲,我真的很高兴一切正常。但是,将我的应用程序放入云中(前端为 AWS Amplify,后端为 EKS)后,问题出现了。甚至我的扩展也无助于解决它。显然,我在使用 AWS Amplify 而不是 localhost 时遇到了另一种 CORS 问题。
这是一个连扩展都没有解决的问题(也没有编码)。从逻辑上讲,我的扩展没有帮助我并不是直接的问题。但是,这表明我只是遇到了另一种 CORS 问题。
我有什么不同类型的 CORS 问题,因为扩展没有解决它?
以及如何解决这个问题?
您需要将 content-type
header 添加到您的 CORS 策略:
builder.WithOrigins("http://example.com")
.WithHeaders(HeaderNames.ContentType) // Add your allowed headers here
.WithMethods("PUT", "DELETE", "GET", "OPTIONS");
在 NetCore 项目的 Startup.cs 文件中,转到配置部分并添加:
app.UseCors(builder =>
{
builder.SetIsOriginAllowedToAllowWildcardSubdomains()
.WithOrigins("http://example.com") //Put your React app url here
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
在同样的情况下它对我有用。
我有一个工作的 React 应用程序,它的工作后端是用 .NET Core 制作的。我遇到了 CORS 问题。清楚记录的错误。最初,我通过使用 chrome 扩展名 (this one) as explained here.
“解决”了这个问题后来,我尝试了一个更好的选择,allowing CORS via code in my .NET Core application。这解决了我的问题。我的 React 应用程序(使用 localhost:3000)能够使用后端 (localhost:5000)。不再需要扩展。
从逻辑上讲,我真的很高兴一切正常。但是,将我的应用程序放入云中(前端为 AWS Amplify,后端为 EKS)后,问题出现了。甚至我的扩展也无助于解决它。显然,我在使用 AWS Amplify 而不是 localhost 时遇到了另一种 CORS 问题。
这是一个连扩展都没有解决的问题(也没有编码)。从逻辑上讲,我的扩展没有帮助我并不是直接的问题。但是,这表明我只是遇到了另一种 CORS 问题。
我有什么不同类型的 CORS 问题,因为扩展没有解决它?
以及如何解决这个问题?
您需要将 content-type
header 添加到您的 CORS 策略:
builder.WithOrigins("http://example.com")
.WithHeaders(HeaderNames.ContentType) // Add your allowed headers here
.WithMethods("PUT", "DELETE", "GET", "OPTIONS");
在 NetCore 项目的 Startup.cs 文件中,转到配置部分并添加:
app.UseCors(builder =>
{
builder.SetIsOriginAllowedToAllowWildcardSubdomains()
.WithOrigins("http://example.com") //Put your React app url here
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
在同样的情况下它对我有用。