flutter web app fails to connect to my rest api , says
Access to XMLHttpRequest at
‘https://example.com:5050/api/Data/GetData’ from origin
‘https://example.com’ has been blocked by CORS policy: Response
to preflight request doesn’t pass access control check: No
‘Access-Control-Allow-Origin’ header is present on the requested
resource.
previously I had issue with app not retrieving data from my backend due to data sent over http, but now shows this
“No ‘Access-Control-Allow-Origin’ header is present”.
Any Help will be highly appreciated
You need to configure CORS policy on server side. For developement purposes if your backend is .NET Core:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "CorsPolicy",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
/*...*/
var app = builder.Build();
app.UseCors("CorsPolicy");
You can also look at this thread:
https://github.com/jasontaylordev/CleanArchitecture/issues/467
Some users report, that moving: app.UseHttpsRedirection();
after app.UseCors();
resolved problem.