asp.net MVC jsonp跨域获取数据

public class JsonpResult : JsonResult
    {        
        object _data = null;
        public JsonpResult() { }
        public JsonpResult(object data)
        {
            this._data = data;
        }
        public override void ExecuteResult(ControllerContext context)
        {
            if(context != null)
            {
                HttpResponseBase response = context.HttpContext.Response;
                HttpRequestBase request = context.HttpContext.Request;
                string callbackFunction = request["callback"];
          HttpContext.Current.Response.AddHeader("p3p", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
if(string.IsNullOrEmpty(callbackFunction)) { throw new Exception("callback function name is empty"); } response.ContentType = "application/x-javascript"; if(_data != null) { string strjson = Newtonsoft.Json.JsonConvert.SerializeObject(_data); response.Write(string.Format("{0}({1});", callbackFunction, strjson)); } } } }
public class apiController : Controller
    {
        // GET: api
        /// <summary>
        /// 登入账号
        /// </summary>
        /// <returns></returns>
        public JsonpResult getUserName()
        {
            return new JsonpResult(new { name = User.Identity.Name });
        }
        /// <summary>
        /// 是否已经登入,已登入islogin=1,否则为0
        /// </summary>
        /// <returns></returns>
        public JsonpResult isUserLogin()
        {
            return new JsonpResult(new { islogin = Request.IsAuthenticated ? 1 : 0 });
        }
    }

client:

<script type="text/javascript">
        var hxsso = function (url) {
            return {
                checkIsLogin: function (callbackfun) {
                    $.ajax({
                        url: url + "/api/isUserLogin",
                        type: "GET",
                        dataType: "jsonp",
                        jsonp: "callback",
                        success: function (data) {
                            if (callbackfun) {
                                callbackfun(data.islogin);
                            }
                        }
                    });
                },
                loginName: function (callbackfun) {
                    $.ajax({
                        url: url + "/api/getUserName",
                        type: "GET",
                        dataType: "jsonp",
                        jsonp: "callback",
                        success: function (data) {
                            if (callbackfun) {
                                callbackfun(data.name);
                            }
                        }
                    });
                }
            }
        }
        $(function () {
            var ssoObj = hxsso("http://192.168.16.241:8520");
            //var ssoObj = new hxsso("http://www.2.sso.com");
            ssoObj.checkIsLogin(loginOnCallBack);
            function loginOnCallBack(islogin) {
                var url = "";
                if (islogin) {
                    $("#ploginOut").show();
                    $("#plogin").hide();
                    if ("@isLogin" == "0") {//sso已经登入,本地未登入,这就需要本地保存自己的cookie。
                        ssoObj.loginName(setUserCookie);
                    }
                }
                else {
                    $("#ploginOut").hide();
                    $("#plogin").show();
                    if ("@isLogin" == "1") {//sso未登入,本地确已经登入,这就需要注销本地的。
                        url = "/Home/RemoveUserCookie";
                        $.ajax({
                            url: url,
                            type: "GET",
                            success: function (data) {
                            }
                        });
                    }
                }
            }
            function setUserCookie(name) {
                $.ajax({
                    url: "/Home/SetUserCookie?name=" + name,
                    type: "GET",
                    success: function (data) {
                    }
                });
            }
        })

    </script>

 

posted @ 2015-06-25 11:21  英雄饶命啊  阅读(380)  评论(0编辑  收藏  举报