码迷,mamicode.com
首页 > Web开发 > 详细

Jsonp类

时间:2016-07-29 21:21:50      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

 public class JsonpResult : JsonResult
    {
        public JsonpResult()
        {
            this.Callback = "callback";
        }

        public JsonpResult(string callback)
        {
            this.Callback = callback;
        }

        /// <summary>
        /// Jsonp 回调的 function 名称,默认为 callback
        /// </summary>
        public string Callback { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            var request = context.HttpContext.Request;
            var response = context.HttpContext.Response;
            string jsoncallback = (context.RouteData.Values[this.Callback] as string) ?? request[this.Callback];
            if (!string.IsNullOrEmpty(jsoncallback))
            {
                if (string.IsNullOrEmpty(base.ContentType))
                {
                    base.ContentType = "application/x-javascript";
                }
                response.Write(string.Format("{0}(", jsoncallback));

     
                response.Headers.Add("P3P", "CP=CAO PSA OUR");
            }
            base.ExecuteResult(context);
            if (!string.IsNullOrEmpty(jsoncallback))
            {
                response.Write(")");
            }
        }
    }

 

调用方法

 [HttpGet]
        public JsonpResult GetComContent( string key)
        {
            string data = "hi:"+key;

            return new JsonpResult() { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };

        }

 

Jsonp类

标签:

原文地址:http://www.cnblogs.com/zhshlimi/p/5719622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!