代码描述:ajax无刷新用户登录。ajax无刷新用户登录代码实例
无刷新登录或者提交留言等功能,现在是越来越流行了,如果一个网站没有点ajax的应用实在感觉没有任何技术水平,当然这个技术其实也没有什么难度,下面就是一段这样的代码实例,对于初学者应该是有所帮助。 下面分段贴出代码:
<script> <table> <tr> <td>用户名:</td> <td><input type="text" id="username" /></td> </tr> <tr> <td>密码:</td> <td><input type="text" id="password" /></td> </tr> <tr> <td>验证码:</td> <td><input type="text" id="cord" /> <img alt="点击更换验证码" title="看不清楚,请单击我!" id="checkcord" src="img.ashx" /></td> </tr> <tr> <td><input type="button" value="登录" /></td> <td></td> </tr> </table> </script>
上面是登录信息填写的部分。 二.jQuery代码部分:
<script> function login(){ $.ajax({ url:'Login.ashx', data:'username='+$("#username").val()+"&password="+$("#password").val()+"&cord="+$("#cord").val(), type:'post', error:function(){ alert("链接服务器错误!"); }, success: function(msg){ alert(msg); } }); } //验证码图片 $(function(){ $("#username").focus(); $("#checkcord").click(function(){ $("#checkcord").attr("src","img.ashx?time="+new Date()); }) }) </script>
三.c#后台处理代码如下:
<script> context.Response.ContentType="text/plain"; string username=context.Request.Form["username"]; string password=context.Request.Form["password"]; string cord=context.Request.Form["cord"]; if(context.Session["cord"]!=null) { if(context.Session["cord"].ToString()==cord) { if(username=="admin"&&password=="admin") { context.Response.Write("登录成功!"); } else { context.Response.Write("登录失败!用户名和密码错误!"); } } else { context.Response.Write("验证码错误!"); } } </script>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能