-2

I have an API project in .net with MVC and another project in angular 7. When I try to call a method, the CORS issues occurred. The error message is shown below. Can anyone help to solve the issue? Thanks in Advance

Access to XMLHttpRequest at 'http://localhost:55134/UserDetails/GetUserDetails' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have already installed the package Microsoft.AspNet.WebApi.Cors and added

EnableCorsAttribute cors = new EnableCorsAttribute("*","*","*");
config.EnableCors(cors);

in the WebApiConfig.cs file.

Ajeesh Sathyan
  • 337
  • 2
  • 5
  • 15
  • Possible duplicate of [Deadly CORS when http://localhost is the origin](https://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin) – Mahi Jul 31 '19 at 16:13
  • check this also:https://stackoverflow.com/questions/56015934/no-access-control-allow-origin-header-in-asp-core-and-angular7 – AT82 Jul 31 '19 at 16:15
  • 2
    I don't think this is answerable without a [mcve]. –  Jul 31 '19 at 16:45

2 Answers2

1

Modify the server to add the header Access-Control-Allow-Origin: * to enable cross-origin requests from anywhere (or specify a domain instead of *).

*Dev-Only:

Disable Same Origin If this is only for development or learning purposes, the easiest thing to do is to disable the Same Origin Policy in your browser. Be aware that if you do this, you’re opening your browser up to security risks. Follow these instructions: Chrome Firefox This is more of a last resort. Modifying the server to support CORS or running a proxy are the best approaches.*

For More:- Refer link:- https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/

Deadly CORS when http://localhost is the origin

https://coderwall.com/p/_ff9dg/enabling-cross-origin-resource-sharing-cors-on-google-chrome-for-javascript-api-calls-and-debugging-purposes

or, Use chromium:- Disable same origin policy in Chrome

or,

Add chrome Extension in chrome:- "Allow CORS: Access-Control-Allow-Origin"

Mahi
  • 3,748
  • 4
  • 35
  • 70
-1

There are a few workarounds:

  1. Turn of browser security;
  2. Use proxy (But these are temprory methods);
  3. Set "Access-Control-Allow-Origin":'*' in both server side and request header in angular.

Hope this helps.

fhcimolin
  • 616
  • 1
  • 8
  • 27
  • 1
    `Access-Control-Allow-Origin` is not a request header and will do nothing. –  Jul 31 '19 at 16:20