Kann meine API nicht sehen, dass meine API auf Frontend Console angerufen wird?
Posted: 18 Aug 2025, 14:17
Code: Select all
fetch('https://localhost:7077/api/test/users')
.then(res => res.json())
.then(data => console.log('Users:', data))
.catch(err => console.error('Error:', err));
< /code>
Fehler: < /p>
PromiseĀ {}
(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
Code: Select all
using Microsoft.EntityFrameworkCore;
using Zama.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Add DbContext
builder.Services.AddDbContext(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();`