fetch('https://localhost:7077/api/test/users')
.then(res => res.json())
.then(data => console.log('Users:', data))
.catch(err => console.error('Error:', err));
Error:
Promise {<pending>}
(index):1 Access to fetch at 'https://localhost:7077/api/test/users' from origin 'https://farm-link-zimbabwe-gh.vercel.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.Understand this error
VM247:1 GET https://localhost:7077/api/test/users net::ERR_FAILED 200 (OK)
(anonymous) @ VM247:1Understand this error
VM247:4 Error: TypeError: Fa
This is my code in program.cs:
using Microsoft.EntityFrameworkCore;
using Zama.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Add DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowVercel", policy =>
{
policy.WithOrigins("https://farm-link-zimbabwe-gh.vercel.app")
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors("AllowVercel");
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();`
<meta http-equiv="content-security-policy"or something among those lines