ajax实现的无刷新用户登录实例代码:
无刷新登录或者提交留言等功能,现在是越来越流行了,如果一个网站没有点ajax的应用实在感觉没有任何技术水平,当然这个技术其实也没有什么难度,下面就是一段这样的代码实例,对于初学者应该是有所帮助。
下面分段贴出代码:
<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 title="看不清楚,请单击我!" id="checkcord" src='/images/loading.gif' data-original="img.ashx" /></td> </tr> <tr> <td><input type="button" value="登录" /></td> <td></td> </tr></table>
上面是登录信息填写的部分。
二.jQuery代码部分:
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()); })})
三.c#后台处理代码如下:
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("验证码错误!"); } }
以上代码是非常的简单,这里就无需多说了,只需要参看以下相关基础概念介绍局可以了。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=9272
更多内容可以参阅:http://www.softwhy.com/jquery/
原标题:ajax实现的无刷新用户登录实例代码
关键词:ajax