我在 Asp.net 中调用 REST Web API 时遇到以下错误。
XMLHttpRequest 无法加载 http://localhost:54859/api/PostData .对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin” header 。产地' http://localhost:3000 ' 因此不允许访问。
我使用 Angular2 作为前端。在后端,我添加了以下代码以在 WEB API 中启用 CORS。
var corsAttr = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsAttr);
对于 Http get 请求,一切正常,但对于 Http Post 请求,则不一样。
任何帮助将不胜感激
提前致谢!
最佳答案
我通过向 web.config 添加以下行解决了这个问题。
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
谢谢。
关于c# - 对预检请求的响应未通过访问控制检查(Angular2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40547836/