1

My ASP.NET WebAPI call was working perfectly when hosted in debugging mode (I can call from angular, post man etc. with no issues found).

When I host the same WebAPI on IIS and call post methods from Angular, it's giving me an error (I have no issues with the get method). Post and get are working even when calling from post man tool.

192.168.100.12:89/api/Booking/schedules/:1 Failed to load resource: the server responded with a status of 404 (Not Found)

Access to XMLHttpRequest at 'http://192.168.100.12:89/api/Booking/schedules/' from origin 'http://localhost:3000' 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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Configure IIS CORS modulel to handle such preflight requests, https://blogs.iis.net/iisteam/getting-started-with-the-iis-cors-module – Lex Li Mar 12 '19 at 14:10

1 Answers1

0

You are trying to access the url at 192.168.100.12 from localhost:3000. Since they are not at the same address, it is considered cross-origin. By default these requests are blocked as a security measure.

  • If you are using Angular (not angularjs), you can set up a proxy to forward requests during development. See this question for setting up a proxy.
  • Alternatively, you can add a DEBUG flag that will disable your CORS policy.
jle
  • 9,316
  • 5
  • 48
  • 67
  • I am using angular 7 I had added below lines in package.json still no luck . "/api": { "target": "http://localhost:3000", "secure": false, "pathRewrite": {"^/api" : ""} }, – Muhammed Eassa Mar 12 '19 at 13:25
  • You need to add a proxy.json alongside it. – jle Mar 12 '19 at 15:21
  • https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md – jle Mar 12 '19 at 15:22
  • Finally get solution i host web API in another network and call from my system its working fine.... if API host in same system get cors error – Muhammed Eassa Mar 15 '19 at 11:11