ASP. NET MVC Jsonp cross-domain data acquisition

Source: Internet
Author: User

 Public classJsonpresult:jsonresult {Object_data =NULL;  PublicJsonpresult () {} PublicJsonpresult (Objectdata) {             This. _data =data; }         Public Override voidExecuteresult (ControllerContext context) {if(Context! =NULL) {httpresponsebase response=context.                Httpcontext.response; Httprequestbase Request=context.                HttpContext.Request; stringCallbackfunction = request["Callback"]; if(string. IsNullOrEmpty (callbackfunction)) {Throw NewException ("callback function name is empty"); } response. ContentType="Application/x-javascript"; if(_data! =NULL)                {                    stringStrjson =Newtonsoft.Json.JsonConvert.SerializeObject (_data); Response. Write (string. Format ("{0} ({1});", Callbackfunction, Strjson)); }            }                    }    }
 Public classApicontroller:controller {//Get:api        /// <summary>        ///Log in Account/// </summary>        /// <returns></returns>         PublicJsonpresult GetUserName () {return NewJsonpresult (New{name =User.Identity.Name}); }        /// <summary>        ///If you have logged in, you have logged in to Islogin=1, otherwise 0/// </summary>        /// <returns></returns>         PublicJsonpresult Isuserlogin () {return NewJsonpresult (New{IsLogin = request.isauthenticated?1:0 }); }    }

Client

<script type= "Text/javascript" >varHxsso =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 () {            varSsoobj = Hxsso ("http://192.168.16.241:8520"); //var ssoobj = new Hxsso ("http://www.2.sso.com");Ssoobj.checkislogin (Loginoncallback); functionLoginoncallback (islogin) {varurl = ""; if(IsLogin) {$ ("#ploginOut"). Show (); $("#plogin"). Hide (); if("@isLogin" = = "0") {//SSO is already logged in and is not logged in locally, which requires you to save your own cookies locally. Ssoobj.loginname (Setusercookie); }                }                Else {                    $("#ploginOut"). Hide (); $("#plogin"). Show (); if("@isLogin" = = "1") {//SSO is not logged in, the local is logged in, this requires logging off the local. url = "/home/removeusercookie"; $.ajax ({url:url, type:"GET", Success:function(data) {}}); }                }            }            functionSetusercookie (name) {$.ajax ({URL:"/home/setusercookie?name=" +name, type:"GET", Success:function(data) {}}); }        })    </script>

ASP. NET MVC Jsonp cross-domain data acquisition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.