jquery學(xué)習(xí)筆記 用jquery實現(xiàn)無刷新登錄
更新時間:2011年08月08日 23:41:27 作者:
為了防止以后好久不用生疏,在這里記下,供剛開始學(xué)習(xí)jquery的童鞋們借鑒,我也是剛開始學(xué)jquery,有什么寫的不對的地方,還請大家指出錯誤,共同進步。
好了,嘮嗑就到這里,現(xiàn)在看如何用jquery實現(xiàn)無刷新登錄。
首先先創(chuàng)建html的部分
<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" onclick="login();" value="登錄" />
</td>
<td>
</td>
</tr>
</table>
這里面包含的功能有:登錄的驗證,點擊更換驗證碼。這個沒有什么好說的。
下面是jquery的部分
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>-----------別忘了引用這個鏈接,否則jquery不能用
<script type="text/javascript">
//用jquery實現(xiàn)無刷新的登錄驗證
function login() {
$.ajax({
url: 'Login.ashx', //訪問路徑
data: 'username=' + $("#username").val() + "&password=" + $("#password").val() + "&cord=" + $("#cord").val(), //需要驗證的參數(shù)
type: 'post', //傳值的方式
error: function () {//訪問失敗時調(diào)用的函數(shù)
alert("鏈接服務(wù)器錯誤!");
},
success: function (msg) {//訪問成功時調(diào)用的函數(shù),這里的msg是Login.ashx返回的值
alert(msg);
}
});
}
//驗證碼圖片
$(function () {
$("#username").focus();
$("#checkcord").click(function () {
$("#checkcord").attr("src", "img.ashx?time=" + new Date());
});
})
</script>
大概的核心代碼就是這些了,當(dāng)用戶點擊登錄按鈕時,觸發(fā)login事件,用jquery向Login.ashx發(fā)出請求,在Login.ashx當(dāng)中,僅僅只是驗證用戶名和密碼是否匹配,驗證碼是否正確。Login.ashx是用C#語言寫的,如果你們學(xué)習(xí)的是別的語言就將地址更換為別的就可以了。
img.ashx是生成驗證碼的程序,每點擊一次圖片都會重新訪問img.ashx,所以圖片是更換的,在生成圖片的時候,會生成存儲驗證碼的session,在Login.ashx當(dāng)中,判斷用戶輸入的值和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("驗證碼錯誤!");
}
}
這是判斷登錄的代碼。
好了,以上就是核心代碼,希望大家多多指教。也希望我的筆記對您有用
首先先創(chuàng)建html的部分
復(fù)制代碼 代碼如下:
<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" onclick="login();" value="登錄" />
</td>
<td>
</td>
</tr>
</table>
這里面包含的功能有:登錄的驗證,點擊更換驗證碼。這個沒有什么好說的。
下面是jquery的部分
復(fù)制代碼 代碼如下:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>-----------別忘了引用這個鏈接,否則jquery不能用
<script type="text/javascript">
//用jquery實現(xiàn)無刷新的登錄驗證
function login() {
$.ajax({
url: 'Login.ashx', //訪問路徑
data: 'username=' + $("#username").val() + "&password=" + $("#password").val() + "&cord=" + $("#cord").val(), //需要驗證的參數(shù)
type: 'post', //傳值的方式
error: function () {//訪問失敗時調(diào)用的函數(shù)
alert("鏈接服務(wù)器錯誤!");
},
success: function (msg) {//訪問成功時調(diào)用的函數(shù),這里的msg是Login.ashx返回的值
alert(msg);
}
});
}
//驗證碼圖片
$(function () {
$("#username").focus();
$("#checkcord").click(function () {
$("#checkcord").attr("src", "img.ashx?time=" + new Date());
});
})
</script>
大概的核心代碼就是這些了,當(dāng)用戶點擊登錄按鈕時,觸發(fā)login事件,用jquery向Login.ashx發(fā)出請求,在Login.ashx當(dāng)中,僅僅只是驗證用戶名和密碼是否匹配,驗證碼是否正確。Login.ashx是用C#語言寫的,如果你們學(xué)習(xí)的是別的語言就將地址更換為別的就可以了。
img.ashx是生成驗證碼的程序,每點擊一次圖片都會重新訪問img.ashx,所以圖片是更換的,在生成圖片的時候,會生成存儲驗證碼的session,在Login.ashx當(dāng)中,判斷用戶輸入的值和session的值是否相同就可以了。在這里我只顯示主要的源碼了。
復(fù)制代碼 代碼如下:
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("驗證碼錯誤!");
}
}
這是判斷登錄的代碼。
好了,以上就是核心代碼,希望大家多多指教。也希望我的筆記對您有用
您可能感興趣的文章:
- jQuery學(xué)習(xí)筆記之jQuery原型屬性和方法
- jQuery學(xué)習(xí)筆記之jQuery.extend(),jQuery.fn.extend()分析
- jQuery學(xué)習(xí)筆記之jQuery.fn.init()的參數(shù)分析
- jQuery學(xué)習(xí)筆記之jQuery構(gòu)建函數(shù)的7種方法
- jQuery學(xué)習(xí)筆記之總體架構(gòu)
- jQuery學(xué)習(xí)筆記(4)--Jquery中獲取table中某列值的具體思路
- jQuery學(xué)習(xí)筆記 獲取jQuery對象
- jQuery學(xué)習(xí)筆記 操作jQuery對象 文檔處理
- jQuery學(xué)習(xí)筆記 操作jQuery對象 屬性處理
- jQuery學(xué)習(xí)筆記 操作jQuery對象 CSS處理
- jQuery學(xué)習(xí)筆記之toArray()
相關(guān)文章
jQuery zTree搜索-關(guān)鍵字查詢 遞歸無限層功能實現(xiàn)代碼
這篇文章主要介紹了zTree搜索功能 -- 關(guān)鍵字查詢 -- 遞歸無限層的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01JQuery對id中含有特殊字符的轉(zhuǎn)義處理示例
id中包含其他特殊字符比如 /@ 等為了利用jquery獲取該元素需要轉(zhuǎn)義特殊字符,具體實現(xiàn)如下,感興趣的朋友可以參考下2013-09-09