Trouble enabling CORS
I'm building a website whose frontend is in React, and whose backend is in ASP.NET. I am currently trying to test sending a GET request from the frontend to be handled by the backend, but sending the request gives me the error in the picture.
I have tried fixing it using the method outlined here: https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-8.0#cors-with-named-policy-and-middleware
but I still get the same error.
My Program.cs file:
The code I am using to send the request:
Enable Cross-Origin Requests (CORS) in ASP.NET Core
Learn how CORS as a standard for allowing or rejecting cross-origin requests in an ASP.NET Core app.
2 Replies
I am not familiar with react but i can notice one missing code in your cors setup update below line with .AllowAnyHeader()
options.AddPolicy(name: MyAllowSpecificOrigin,
policy =>
{
policy.WithOrigins("http://localhost:5173").AllowAnyMethod().AllowAnyHeader();
});
reason for doing this is sometime in your request you will be passing few extra headers which might be required example, Authorization header etc.
Hope it helps
Nevermind my deleted message, it worked thank you very much