使用以下方法必须注意路径是否正确,这点是绝大部分问题的根源。
取数例子:不刷新获取登录信息
登录前:
登录后:
页面jq:
$("#btnLogin").click(function () { var name = $("#loginName"); var pass = $("#loginPass"); var code = $("#txtCode"); if ($.trim(name.val()) == "") { name.focus(); return alert("请输入你的用户名!"); } if ($.trim(pass.val()) == "") { pass.focus(); return alert("请输入你的密码!"); } if ($.trim(code.val()) == "") { code.focus(); return alert("请输入你的验证码!"); } $.ajax({ contentType: "application/json", url: "/WS/vinson.asmx/Logon", data: "{userName:‘" + name.val() + "‘,userPass:‘" + pass.val() + "‘,code:‘" + code.val() + "‘}", type: "POST", dataType: "json", success: function (json) { json = eval("(" + json.d + ")"); if (json.success == "error") { $("#divLogin").show(); alert(json.msg); code.val(""); pass.val(""); pass.focus(); UpdateImage(); } else if (json.success == "success") { $("#imgFace").attr("src", "/gamepic/face" + json.fid + ".gif"); var name = SubString(json.account, 12, ""); $("#dlInfo").html("<span>帐号:" + name + "</span><span>ID:" + json.gid + "</span>").attr("title", json.account); $("#mOrder").html(json.morder); $("#loves").html(json.loves); $("#mPat a").before(json.UserPat+" "); $("#counts").html(json.counts); if (parseInt(json.dayCount) > 0) $("#qd ul").css("background", "url(../images/qiandao_n.png) 0 0px no-repeat"); else $("#qd ul").css("background", "url(../images/qiandao_n.png) 0 -60px no-repeat"); $("#divLogin").hide(); $("#divInfo").show(); } }, error: function (err, ex) { alert(err.responseText); UpdateImage(); } }); })
vinson.asmx文件:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using Game.Entity.Accounts; using Game.Facade; using Game.Utils; using System.Text; using System.Collections.Generic; using Game.Kernel; /// <summary> ///vinson 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class vinson : System.Web.Services.WebService { public vinson () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } private AccountsFacade accountFacade = new AccountsFacade(); private DataControl gData = new DataControl(); /// <summary> /// 用户登录 /// </summary> /// <param name="userName"></param> /// <param name="userPass"></param> /// <param name="code"></param> /// <returns></returns> [WebMethod] public string Logon(string userName, string userPass, string code) { string msg = ""; if (TextUtility.EmptyTrimOrNull(userName) || TextUtility.EmptyTrimOrNull(userPass)) { msg = "抱歉!您输入的用户名或密码错误了。"; return "{success:‘error‘,msg:‘" + msg + "‘}"; } //验证码错误 if (!code.Equals(Fetch.GetVerifyCode(), StringComparison.InvariantCultureIgnoreCase)) { msg = "抱歉!您输入的验证码错误了。"; return "{success:‘error‘,msg:‘" + msg + "‘}"; } Message umsg = accountFacade.Logon(userName, userPass); if (umsg.Success) { UserInfo ui = umsg.EntityList[0] as UserInfo; ui.LogonPass = TextEncrypt.EncryptPassword(userPass); Fetch.SetUserCookie(ui.ToUserTicketInfo()); string mOrder = ui.MemberOrder == 0 ? "普通会员" : ui.MemberOrder == 1 ? "蓝钻会员" : ui.MemberOrder == 2 ? "黄钻会员" : ui.MemberOrder == 3 ? "白钻会员" : "红钻会员"; int UserPat = (int)gData.SelectValue("select UserPat from AccountsInfo where UserID=" + ui.UserID, "DBAccounts", 0); int counts = (int)gData.SelectValue("select count(*) from RecordSignIn where UserID=" + ui.UserID + " and month(CollectDate)=" + System.DateTime.Now.Month, "DBRecord", 0); int dayCount = (int)gData.SelectValue("select count(*) from RecordSignIn where UserID=" + ui.UserID + " and convert(varchar(100),CollectDate,23)=‘" + System.DateTime.Now.ToString("yyyy-MM-dd") + "‘", "DBRecord", 0); msg = "{success:‘success‘,account:‘" + ui.Accounts + "‘,gid:‘" + ui.GameID + "‘,dayCount:‘" + dayCount + "‘,counts:‘" + counts + "‘,UserPat:‘" + UserPat + "‘,loves:‘" + ui.LoveLiness + "‘,morder:‘" + mOrder + "‘,fid:‘" + ui.FaceID + "‘}"; } else { msg = "{success:‘error‘,msg:‘" + umsg.Content + "‘}"; } return msg; } /// <summary> /// 获取用户登录信息 /// </summary> /// <returns></returns> [WebMethod] public string GetUserInfo() { UserTicketInfo userTick = Fetch.GetUserCookie(); if (userTick == null) return "{}"; Message umsg = accountFacade.GetUserGlobalInfo(userTick.UserID, 0, ""); if (umsg.Success) { UserInfo ui = umsg.EntityList[0] as UserInfo; string mOrder = ui.MemberOrder == 0 ? "普通会员" : ui.MemberOrder == 1 ? "蓝钻会员" : ui.MemberOrder == 2 ? "黄钻会员" : ui.MemberOrder == 3 ? "白钻会员" : "红钻会员"; int UserPat = (int)gData.SelectValue("select UserPat from AccountsInfo where UserID=" + ui.UserID, "DBAccounts", 0); int counts = (int)gData.SelectValue("select count(*) from RecordSignIn where UserID=" + ui.UserID + " and month(CollectDate)=" + System.DateTime.Now.Month, "DBRecord", 0); int dayCount = (int)gData.SelectValue("select count(*) from RecordSignIn where UserID=" + ui.UserID + " and convert(varchar(100),CollectDate,23)=‘" + System.DateTime.Now.ToString("yyyy-MM-dd") + "‘", "DBRecord", 0); return "{success:‘success‘,account:‘" + ui.Accounts + "‘,gid:‘" + ui.GameID + "‘,dayCount:‘" + dayCount + "‘,counts:‘" + counts + "‘,UserPat:‘" + UserPat+"‘,loves:‘" + ui.LoveLiness + "‘,morder:‘" + mOrder + "‘,fid:‘" + ui.FaceID + "‘}"; } return "{}"; } /// <summary> /// 检测用户名 /// </summary> /// <param name="userName"></param> /// <returns></returns> [WebMethod] public string CheckName(string userName) { Message umsg = accountFacade.IsAccountsExist(userName); if (umsg.Success) { return "{success:‘success‘}"; } return "{success:‘error‘,msg:‘" + umsg.Content + "‘}"; } /// <summary> /// 检测昵称 /// </summary> /// <param name="userName"></param> /// <returns></returns> [WebMethod] public string CheckNickName(string nickName) { if (accountFacade.IsNickNameExist(nickName)) { return "{success:‘success‘}"; } return "{success:‘error‘}"; } /// <summary> /// 用户魅力排名 /// </summary> /// <returns></returns> [WebMethod] public string GetUserLoves() { StringBuilder msg = new StringBuilder(); IList<UserInfo> users = accountFacade.GetUserInfoOrderByLoves(); if (users == null) return "{}"; msg.Append("["); foreach (UserInfo user in users) { msg.Append("{userName:‘" + user.NickName + "‘,loves:‘" + user.LoveLiness + "‘},"); } msg.Remove(msg.Length - 1, 1); msg.Append("]"); return msg.ToString(); } }
可以使用这种方法跳到asmx页面做一些事情,而不仅仅是像上面那样取数:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data; /// <summary> ///Lottery 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class Lottery : System.Web.Services.WebService { public Lottery () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } private DataControl gData = new DataControl(); [WebMethod] public string insertLotteryLog(string LotteryID, string UserID) { string msg = ""; string strClientIP = ""; if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) strClientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new char[] { ‘,‘ })[0]; else strClientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; List<string[]> sqls = new List<string[]>(); //1.插入抽奖记录 string sql = "insert into QPRecordDB.dbo.RecordLotteryLog (UserID,LotteryID,ClientIP,CollectDate) values (" + UserID + "," + LotteryID + ",‘" + strClientIP + "‘,getdate())"; sqls.Add(new string[] { "DBRecord", sql }); if (gData.ExecuteMultiTran(sqls)) { msg = "{success:‘success‘,msg:‘insert success‘}"; } else { msg = "{success:‘error‘,msg:‘insert failed‘}"; } return msg; } [WebMethod] public string LotteryFee(string UserID) { string msg = ""; List<string[]> sqls = new List<string[]>(); //1.扣手续费 long Score = (long)gData.SelectValue("select Score from QPTreasureDB.dbo.GameScoreInfo where UserID=" + UserID, "DBTreasure", 0); if (Score >= 200) { string sql = "update QPTreasureDB.dbo.GameScoreInfo set Score=Score-200 where UserID=" + UserID; sqls.Add(new string[] { "DBTreasure", sql }); if (gData.ExecuteMultiTran(sqls)) { msg = "{success:‘success‘}"; } else { msg = "{success:‘error‘}"; } } else msg = "{success:‘arrears‘}"; return msg; } [WebMethod] public string FeeOver(string UserID,string FeeTime) { string msg = ""; int RecordID = (int)gData.SelectValue("select RecordID from RecordLotteryFee where UserID=" + UserID + " and convert(varchar(100),CollectDate,23)=convert(varchar(100),getdate(),23)", "DBRecord", 0); List<string[]> sqls = new List<string[]>(); string sql = "update QPRecordDB.dbo.RecordLotteryFee set FeeTime=" + FeeTime + " where RecordID=" + RecordID; sqls.Add(new string[] { "DBRecord", sql }); if (gData.ExecuteMultiTran(sqls)) { msg = "{success:‘success‘}"; } else { msg = "{success:‘error‘}"; } return msg; } [WebMethod] public string getLotteryName(string LotteryID) { string msg = ""; string LotteryName = gData.SelectValue("select LotteryName from Lottery where LotteryID=" + LotteryID, "DBNativeWeb", "").ToString(); msg = "{lotteryname:‘" + LotteryName + "‘}"; return msg; } }
ajax与json
时间: 2024-10-16 03:33:46