Fork me on GitHub

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请求是不需要设置跨域的,直接可以访问。

 

posted @ 2017-06-04 14:26  磊哥|www.javacn.site  阅读(1058)  评论(0编辑  收藏  举报