对预检请求的响应未通过访问控制检查(Angular2) [英] Response to preflight request doesn't pass access control check (Angular2)

查看:70
本文介绍了对预检请求的响应未通过访问控制检查(Angular2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用Asp.net中的REST Web API时遇到错误.

I am getting below error on call to REST Web API in Asp.net.

XMLHttpRequest无法加载 http://localhost:54859/api/PostData .对预检请求的响应未通过访问控制检查:在所请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许访问来源' http://localhost:3000 .

XMLHttpRequest cannot load http://localhost:54859/api/PostData. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我正在使用Angular2作为前端.在后端,我添加了以下代码以在WEB API中启用CORS.

I am using Angular2 as Front end. In the back end, I have added following codes to enable CORS in WEB API.

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

对于Http get请求,一切正常,但对于Http Post请求,效果却不一样.

Everything works fine for Http get request,but the same not for Http Post request.

任何帮助都是有意义的

提前谢谢!

推荐答案

我通过在web.config中添加以下几行来解决它.

I got it resolved by adding following lines to web.config.

<system.webServer>
   <modules runAllManagedModulesForAllRequests="true">
   </modules>
</system.webServer>

谢谢.

这篇关于对预检请求的响应未通过访问控制检查(Angular2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆