0

From one side I have an ASP.net web application that contains a web api module. On the same machine I have an angular 2 client application which is consuming the web api controllers crud methods. The angular 2 uses a basic http service with the uri of the localhost with the port number on which the web api runs on. I run the web api application and when the angular 2 tries to consume a Get request, I get this error message (22770 is the port of the web-api, 4200 is the port of the angular 2 application):

Failed to load http://localhost:22770/api/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

Although this is quite a common question, mine is a little bit different: CORS is defined as: Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to let a user agent gain permission to access selected resources from a server on a different origin (domain) than the site currently in use

BUT - I'm working with the same domain - on my local machine (localhost).

What is the problem and how can I solve it ?

Guy E
  • 1,775
  • 2
  • 27
  • 55

2 Answers2

1

OK - Investigated it a little bit and reached the reason for that error message when working on the same local host - It appears that a different port is considered as a different domain. Exceptional addition I noticed: "Internet Explorer does not consider the port when comparing origins"

Guy E
  • 1,775
  • 2
  • 27
  • 55
0

You need to enable CORS (cross origin request) in your web api. Follow instruction given in below page

https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Anil
  • 1,669
  • 5
  • 19
  • 44