jsonp

来源:互联网 发布:linux服务器哪里的好 编辑:程序博客网 时间:2024/06/06 03:12
/// <reference path="jquery-1.8.3.min.js" />


var szzcObj = new function () {


    //程序地址
    var ashxUrl = "";



    this.GetAjax = function (postUrl, postData, postDataType, returnDataType, CheckSpan, Endfunction, Errorfunction, Async, strType, successBackFun) {
        if (returnDataType == "")
        { returnDataType = "html"; }
        if (postDataType == "")
        { postDataType = "application/x-www-form-urlencoded"; }
        else if (postDataType == "json")
        { postDataType = "application/json; charset=utf-8"; }
        var async0 = true;
        if (Async == false)
            async0 = false;
        if (CheckSpan && CheckSpan != "") {
            if (!CheckSpan.selector)
                CheckSpan = $("#" + CheckSpan);
            if (CheckSpan.length == 0)
                CheckSpan = "";
        }
        else {
            CheckSpan = "";
        }
        if (!strType)
            strType = "POST";
        if (!successBackFun)
            successBackFun = "success_jsonpCallback";
        $.ajax({
            type: strType,
            url: postUrl,
            data: postData, //"begin=1&end=9"
            contentType: postDataType,  //如:"application/x-www-form-urlencoded" 'application/json; charset=utf-8'
            dataType: returnDataType,          //如:"xml" "html" "json" "script"
            jsonpCallback: successBackFun,
            jsonp: "callbackparam",
            beforeSend: function () {
                if (CheckSpan != "") {
                    CheckSpan.css('display', 'block');
                }
            },
            success: function (data) {
                if (CheckSpan != "") {
                    CheckSpan.css('display', 'none');
                }
                Endfunction(data);
            },
            error: function (xhttp) {
                if (CheckSpan != "") {
                    CheckSpan.css('display', 'none');
                }
                if (Errorfunction && Errorfunction != "")
                    Errorfunction(xhttp);
            },
            async: async0
        });


    };



    this.login = function () {
        var d = {
            sort: "login",
            name: encodeURIComponent($("#txtName").val()),
            pwd: encodeURIComponent($("#txtPwd").val()),
            vcode: encodeURIComponent($("#txtVcode").val())
        };
        if (!szzcObj.canSubmit) {
            return;
        }
        else {
            szzcObj.canSubmit = false;
            this.GetAjax(ashxUrl, d, "json", "jsonp", "",
                function (data) {
                    szzcObj.canSubmit = true;
                    if (data.result == "1") {
                        window.location = mailboxUrl;
                    }
                    if (data.result == "2") {
                        alert("验证码失败")
                    }
                    if (data.result == "0") {
                        alert("登录失败");
                    }
                },
                function (ex) {
                    szzcObj.canSubmit = true;
                }, true, "GET", "jsonpname");
        }


    }

}

         szzcObj.canSubmit = true; //防止重复提交





//后台应用

#region 登录
        void Login(HttpContext context)
        {
            string strUser = context.Server.UrlDecode(context.Request["name"].ToString());
            string strPwd = context.Server.UrlDecode(context.Request["pwd"].ToString());
            strPwd = Maticsoft.DBUtility.Function.md5(strPwd, 16);
            string strVcode = context.Server.UrlDecode(context.Request["vcode"].ToString());
            string vcode = context.Session["CheckCode"] == null ? "" : context.Session["CheckCode"].ToString();
            string jsonpname = context.Request["callbackparam"];
            if (strVcode == vcode)
            {
                OutUserInfo userinfo = new OutUserInfo();
                OutUserInfo_BLL outuserinfo_Bll = new OutUserInfo_BLL();
                bool b = false;
                b = outuserinfo_Bll.UserLogin(strUser, strPwd);
                if (b)
                {
                    userinfo = outuserinfo_Bll.GetModelByUserNameAndPwd(strUser, strPwd);
                    context.Session["loginName"] = strUser;
                    context.Session["loginnum"] = userinfo.LOGINTIMES.ToString();
                    context.Session["logintime"] = userinfo.LASTLOGIN.ToString();
                    context.Session["userid"] = userinfo.OUTUSERID.ToString();
                    context.Session["userInfo"] = Maticsoft.DBUtility.Function.md5(strPwd, 16) + "_" + userinfo.OUTUSERID.ToString();
                    userinfo.LOGINTIMES = userinfo.LOGINTIMES + 1;
                    userinfo.LASTLOGIN = DateTime.Now;
                    outuserinfo_Bll.Updae(userinfo, false);
                    context.Response.Write(jsonpname + "({result:1})");
                }
                else
                {
                    context.Response.Write(jsonpname + "({result:0})");
                }
            }
            else
            {
                context.Response.Write(jsonpname + "({result:2})");
            }
        }


        void NewLogin(HttpContext context) 
        {
            string strUser = context.Server.UrlDecode(context.Request["name"].ToString());
            string strPwd = context.Server.UrlDecode(context.Request["pwd"].ToString());
            strPwd = Maticsoft.DBUtility.Function.md5(strPwd, 16);
            string strVcode = context.Server.UrlDecode(context.Request["vcode"].ToString());
            string vcode = context.Session["CheckCode"] == null ? "" : context.Session["CheckCode"].ToString();
            if (strVcode == vcode)
            {
                OutUserInfo userinfo = new OutUserInfo();
                OutUserInfo_BLL outuserinfo_Bll = new OutUserInfo_BLL();
                bool b = false;
                b = outuserinfo_Bll.UserLogin(strUser, strPwd);
                if (b)
                {
                    userinfo = outuserinfo_Bll.GetModelByUserNameAndPwd(strUser, strPwd);
                    context.Session["loginName"] = strUser;
                    context.Session["loginnum"] = userinfo.LOGINTIMES.ToString();
                    context.Session["logintime"] = userinfo.LASTLOGIN.ToString();
                    context.Session["userid"] = userinfo.OUTUSERID.ToString();
                    context.Session["userInfo"] = Maticsoft.DBUtility.Function.md5(strPwd, 16) + "_" + userinfo.OUTUSERID.ToString();
                    userinfo.LOGINTIMES = userinfo.LOGINTIMES + 1;
                    userinfo.LASTLOGIN = DateTime.Now;
                    outuserinfo_Bll.Updae(userinfo, false);


                    StringBuilder sb = new StringBuilder();
                    sb.Append(context.Session["loginName"].ToString() + ",");
                    sb.Append(context.Session["loginnum"].ToString() + ",");
                    sb.Append(context.Session["logintime"].ToString() + ",");
                    sb.Append(context.Session["userid"].ToString());
                    string str = sb.ToString();
                    WriteText(str);


                    context.Response.Write("1");




                }
                else
                {
                    context.Response.Write("0");
                }
            }
            else
            {
                context.Response.Write("2");
            }
        }
        #endregion




//jquery    jsonp  


 function doJq()
        {
            //使用 jq 发送跨域请求
            $.ajax(requestUrl, {
                type:"get",
                dataType: "jsonp",// 指定 jq 发送 jsonp 请求(内部,就是动态创建一个 script 标签)
                jsonp: "callbackFun",// 指定 传递 函数名的 参数的名字 如: url?callbackFun = fun
                jsonpCallback: "fun",// 指定 回调 的 函数名
                success: function () {
                    alert("123123");
                }
            });
        }


    // 回调函数
        function fun(data)
        {
            for (var key in data)
            {
                alert(data[key]);
            }
        }


//js 标签


   <!--Script标签可以跨域访问-->
    <script type="text/javascript" src="http://www.oumind.com/C01Target.ashx?callback=fun"></script>


callback=fun  回调函数




0 0
原创粉丝点击