[英]Communicating between React and Asp.net core project
I am getting this error when i try and connect my react project to my asp.net core 3 back end当我尝试将我的反应项目连接到我的 asp.net 核心 3 后端时出现此错误
No 'Access-Control-Allow-Origin' header is present on the requested resource
I have up this is my startup method ConfigureServices我已经起来了这是我的启动方法ConfigureServices
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin",
builder => builder.AllowAnyOrigin());
});
And I have also put this is the startup config method aswell我也把这也是启动配置方法
app.UseCors("AllowOrigin");
Add these:添加这些:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
It doesn't look right what you did.你所做的看起来不正确。 The name of your cors policy and cors policy you use are not same.
您使用的 cors 策略和 cors 策略的名称不同。 It has to be same.
它必须相同。 Should be:
应该:
app.UseCors("AllowOrigin");
Or what ever your cors policy name.或者您的 cors 政策名称是什么。
But also allowing any origins are dangerous.但也允许任何起源都是危险的。 It is better you specified with origin can access this api.
最好用 origin 指定可以访问这个 api。 I give an example:
我举个例子:
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://example.com",
"http://www.contoso.com");
});
});
// services.AddResponseCaching();
services.AddControllers();
}
And than:然后:
app.UseCors(MyAllowSpecificOrigins);
Two issues.两个问题。
Once was as describe above.曾经是如上所述。 The second is that you have to use https instead of http.
第二个是您必须使用 https 而不是 http。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.