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

      

  • 相关阅读:
    asp.net程序集冲突解决笔记(未能加载文件或程序集"XXXXXXXXX")
    让Asp.net mvc WebAPI 支持OData协议进行分页查询操作
    jQuery Select 自动选择默认值
    nuget在jenkins上不能自动还原项目依赖包---笔记
    Ubuntu 14.04 server ssh 远程服务遇到的一点事儿
    Unbunt vi 编辑器键盘按键不正确的一次经历与解决方案
    Ubuntu root 密码 sudo passwd
    Visual Studio 2015 下 编译 libpng
    .NET使用Com组件的一点点教训笔记~
    Linux透明大页(Transparent Huge Pages)对ES性能对影响
  • 原文地址:https://www.cnblogs.com/gavinhuang/p/5458175.html
Copyright © 2011-2022 走看看