zoukankan      html  css  js  c++  java
  • 跨域

    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 数据,输出回调函数
            }
    

      

  • 相关阅读:
    C# 获取命名空间对应的程序集位置
    启用/禁用以太网的批处理,用于一个网卡切换本地网络和wifi使用(Win10)
    Gogs/Gitea 在 docker 中部署
    bash echo color
    python运行httpserver
    更改当前启动项,开关Hyper-V
    Win10更改CMD控制台的代码页和字体和字号
    ubuntu 编译 vim+lua
    CPU Usage (C#) 测试
    单击改变input的边框颜色
  • 原文地址:https://www.cnblogs.com/gavinhuang/p/5458175.html
Copyright © 2011-2022 走看看