wcf项目跨域问题处理

  最近做了一个wcf项目,请求发起的项目是一个webform项目,所以这是分开的两个项目端口必然不一样,理所当然存在跨域问题。

  有的人当下就反应过来jsonp,jsonp只能用于get请求,对于参数比较多的后台系统想想url后面挂着一排参数好像也不太美观。

  我找了一段时间的资料貌似在前端没有处理空间了也就是放弃了在webform端处理跨域的这种办法。

  解决方案:在wcf端添加Golbal.asax文件然后

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");

                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
            
        }
原文地址:https://www.cnblogs.com/AndyLin/p/11423529.html