Reputation: 35
https://pastebin.com/Uy9r8bEP I am trying to connect web api with flutter web and while it works in mobile it doesnt work in web. I have read and found out the reason is because of CORS. I tried enabling it from flutter and web API side (for web api im using asp.net core web api) and it didn't work. Then I tried disabling it from both sides. Still didnt work. This is the header I have
headers: {
'Content-Type': 'application/json',
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
"Access-Control-Allow-Credentials": "true", // Required for cookies, authorization headers with HTTPS
"Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
"Access-Control-Allow-Methods": "POST, OPTIONS"
},
Dart/Flutter: Http request raises XMLHttpRequest error I even added disable web security (made sure to add the comma too) but it still didn't work. Does anyone have any other suggestions as to how to make it work?
Upvotes: 0
Views: 1050
Reputation: 1389
On Flutter Web get your launcher on a fixed port.
And add the fixed domain name to the allowed fields like this.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", builder => {
builder.AllowAnyMethod().AllowAnyHeader().WithOrigins(
"https://localhost:7261",
// add here
"http://localhost:52960")
.AllowCredentials();
}));
var app = builder.Build(); app.UseCors("CorsPolicy");
VsCode launch.json
"configurations": [
{
"name": "name",
"request": "launch",
"type": "dart"
},
{
"name": "name (const port)",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"args": ["--web-port=52960"]
},
{
"name": "name (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
Upvotes: 2