Reputation: 127
In my react app this will shows. when I try to make a post request. I'm using .net core 2.2 webapi for the backend.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5050/api/Users. (Reason: missing token ‘access-control-allow-origin’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel).[Learn More]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5050/api/Users. (Reason: CORS request did not succeed).[Learn More]
In my .net core webapi. I have enabled the cors as well.
using IServiceCollection services
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
and IApplicationBuilder app
app.UseCors("AllowAll");
also I used this in controller.
[EnableCors("AllowAll")]
Edit: On debug console output.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 OPTIONS http://localhost:5050/api/Users
Request starting HTTP/1.1 OPTIONS http://localhost:5050/api/Users
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executing endpoint '405 HTTP Method Not Supported'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executed endpoint '405 HTTP Method Not Supported'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 9.3884ms 405
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 9.3884ms 405
Upvotes: 2
Views: 2616
Reputation: 127
First I should thank all who tried to help me. I have solved the issue.
First I uninstalled the IIS using Add and Remove Programs.
And I also I have turned off the IIS feature from Windows Features.
It requires to restart the PC. After that, that issue doesn't happen.
Upvotes: 0
Reputation: 2550
If you're using full IIS and having issues, make sure you rule out:
IIS > App or Directory \ HTTP Response Headers \ Access-Control-Allow-Origin
Being set already. I had an issue whereby it was already set for some reason and was overriding the app's config.
Upvotes: 1
Reputation: 2452
The first thing to check is if this error message is not the result of an error in the web api.
To do this run chrome (windows) in a special mode
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:/Chrome dev session" --disable-web-security
If you still get the error then I suspect it might be due to some other error in your web api. Have you checked the api console for any exceptions?
Cheers
Upvotes: 2
Reputation: 16
Try Response.Headers.Add("Access-Control-Allow-Origin", "http://IPorDomain:Port"); It works in .Net Core Web API. In my case i am consuming the Web api from angular.
var itemsOnPage = await _context.Employees
.OrderBy(e => e.EmpFirstName)
.Skip(count * pageIndex)
.Take(count)
.ToListAsync();
/*This*/
Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:4200");
/*This*/
return Ok(itemsOnPage);
Upvotes: 0