跨域

chrome iframe方案

 <script type="text/javascript">
        //var $ = jQuery;
        try{
            document.domain = "xlobo.com";
        }
        catch(ex){

        }
    </script>

  

 System.Web.Helpers.AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

  

 <httpProtocol>
      <customHeaders>
        <add name="X-Frame-Options" value="ALLOW-FROM:http://localhost:18592/Home/Index" />
        <add name="Access-Control-Allow-Origin" value="Access-Control-Allow-Origin: *" />
      </customHeaders>
    </httpProtocol>

jsonp

<script type="text/javascript">
        window.onload = function () {
            $.ajax({
                type: "GET",
                url: "http://localhost:18592/Home/Index2?callback=?",    //跨域url
                dataType: "jsonp",  //指定 jsonp 请求
                success: function (data) {
                    location.href = "/Home/SetCookie?id=" + data.ID + "&name=" + data.Name;
                }
            });
        };

  

   public ActionResult SetCookie(string id = "", string name = "")
        {
            if (id != "" && name != "")
            {
                var cookie = new HttpCookie(CookieName) { Value = string.Format("{0},{1}", id, System.Web.HttpContext.Current.Server.UrlEncode(name)) };
                ControllerContext.HttpContext.Response.Cookies.Add(cookie);
            }
            return RedirectToAction("Index");
        }

  

public class JSONPResult : JsonResult
        {
            public JSONPResult()
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            }

            public string Callback { get; set; }

            ///<summary>
            ///对操作结果进行处理
            ///</summary>

            ///<paramname="context"></param>

            public override void ExecuteResult(ControllerContext context)
            {
                var httpContext = context.HttpContext;
                var callBack = Callback;

                if (string.IsNullOrWhiteSpace(callBack))
                    callBack = httpContext.Request["callback"]; //获得客户端提交的回调函数名称

                httpContext.Response.ContentType = "application/json";

                // 返回客户端定义的回调函数
                httpContext.Response.Write(callBack + "(");
                httpContext.Response.Write(Data);          //Data 是服务器返回的数据        
                httpContext.Response.Write(");");            //将函数输出给客户端,由客户端执行
            }
        }

        public ActionResult Index2()
        {
            //获取回调函数名
            string callback = HttpContext.Request.QueryString["callback"];

            var cookie = ControllerContext.HttpContext.Request.Cookies[CookieName];
            string Id = System.Web.HttpContext.Current.Server.UrlDecode(cookie.Value.Split(',')[0]);
            string nm = System.Web.HttpContext.Current.Server.UrlDecode(cookie.Value.Split(',')[1]);

            var str = new StringBuilder();
            str.Append("{");
            str.Append(""ID":"" + Id + """);
            str.Append(",");
            str.Append(""Name":"" + nm + """);
            str.Append("}");

            return new JSONPResult { Data = str };  //返回 jsonp 数据,输出回调函数
        }

  

原文地址:https://www.cnblogs.com/gavinhuang/p/5458175.html