Home  >  Article  >  Web Front-end  >  jquery学习笔记 用jquery实现无刷新登录_jquery

jquery学习笔记 用jquery实现无刷新登录_jquery

WBOY
WBOYOriginal
2016-05-16 18:03:451416browse

好了,唠嗑就到这里,现在看如何用jquery实现无刷新登录。
首先先创建html的部分

复制代码 代码如下:



















用户名:



密码:



验证码:


点击更换验证码





这里面包含的功能有:登录的验证,点击更换验证码。这个没有什么好说的。
下面是jquery的部分
复制代码 代码如下:

-----------别忘了引用这个链接,否则jquery不能用


大概的核心代码就是这些了,当用户点击登录按钮时,触发login事件,用jquery向Login.ashx发出请求,在Login.ashx当中,仅仅只是验证用户名和密码是否匹配,验证码是否正确。Login.ashx是用C#语言写的,如果你们学习的是别的语言就将地址更换为别的就可以了。
img.ashx是生成验证码的程序,每点击一次图片都会重新访问img.ashx,所以图片是更换的,在生成图片的时候,会生成存储验证码的session,在Login.ashx当中,判断用户输入的值和session的值是否相同就可以了。在这里我只显示主要的源码了。
复制代码 代码如下:

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("验证码错误!");
}
}

这是判断登录的代码。
好了,以上就是核心代码,希望大家多多指教。也希望我的笔记对您有用
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn