首页 > 解决方案 > 在 IIS 上托管后从 Angular 调用时出现 WebAPI 错误(仅限 Post 方法)

问题描述

在调试模式下托管时,我的 ASP.NET WebAPI 调用运行良好(我可以从 angular、post man 等调用,没有发现问题)。

当我在 IIS 上托管相同的 WebAPI 并从 Angular 调用 post 方法时,它给了我一个错误(我对 get 方法没有任何问题)。即使从邮递员工具调用,Post 和 get 也能正常工作。

192.168.100.12:89/api/Booking/schedules/:1 加载资源失败:服务器响应状态为 404(未找到)

从源“http://localhost:3000”访问“http://192.168.100.12:89/api/Booking/schedules/”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

标签: asp.netangulariisasp.net-web-api

解决方案


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.

推荐阅读