首页 > 代码库 > WebApi开启CORS支持跨域POST
WebApi开启CORS支持跨域POST
概念:CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing)。它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制。
现象:如请求出现:Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Origin‘ header is present on the requested resource. Origin ‘xxxxx‘ is therefore.
WebApi开启CORS设置分为2步:
Step1:打开NuGet安装Microsoft.AspNet.Cors 如图:
Step2:WebApiConfig.cs设置开启Cors,代码如下:
//跨域配置config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*"));
即可解决Post跨域的问题,当然如果是Get请求是不需要设置跨域的,直接可以访问。
WebApi开启CORS支持跨域POST
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。