基于jquery ajax 用戶無(wú)刷新登錄方法詳解
更新時(shí)間:2012年04月28日 12:13:16 作者:
ajax俗稱無(wú)刷新登錄或局部刷新登錄,這樣可以提高用戶體驗(yàn)了,文章講到了關(guān)于基于jquery ajax用法返回?cái)?shù)據(jù)與實(shí)例2用戶無(wú)刷新登錄
Ajax框架就是提供模塊化實(shí)現(xiàn)Ajax功能的集合,Ajax框架可以是各種語(yǔ)言實(shí)現(xiàn)的(比如SAJAX有各種語(yǔ)言的實(shí)現(xiàn)),Ajax只是jquery中的一部分,
實(shí)例1
$.ajax({
type:'post',//可選get
url:'action.php',//這里是接收數(shù)據(jù)的PHP程序
data:'data='dsa',//傳給PHP的數(shù)據(jù),多個(gè)參數(shù)用&連接
dataType:'text',//服務(wù)器返回的數(shù)據(jù)類型 可選XML ,Json jsonp script html text等
success:function(msg){
//這里是ajax提交成功后,PHP程序返回的數(shù)據(jù)處理函數(shù)。msg是返回的數(shù)據(jù),數(shù)據(jù)類型在dataType參數(shù)里定義!
},
error:function(){
ajax提交失敗的處理函數(shù)!
}
})
PHP action.php
<?php
echo 'www.dhdzp.com!';
?>
下面看個(gè)jquery ajax用戶無(wú)刷新登錄實(shí)例
實(shí)例2
<html>
<head>
<title>jQuery Ajax 實(shí)例演示</title>
</head>
<script src="./js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){//這個(gè)就是jQueryready ,它就像C語(yǔ)言的main 所有操作包含在它里面
$("#button_login").mousedown(function(){
login(); //點(diǎn)擊ID為"button_login"的按鈕后觸發(fā)函數(shù) login();
});
});
function login(){ //函數(shù) login();
var username = $("#username").val();//取框中的用戶名
var password = $("#password").val();//取框中的密碼
$.ajax({ //一個(gè)Ajax過(guò)程
type: "post", //以post方式與后臺(tái)溝通
url : "login.php", //與此php頁(yè)面溝通
dataType:'json',//從php返回的值以 JSON方式 解釋
data: 'username='+username+'&password='+password, //發(fā)給php的數(shù)據(jù)有兩項(xiàng),分別是上面?zhèn)鱽?lái)的u和p
success: function(json){//如果調(diào)用php成功
//alert(json.username+'n'+json.password); //把php中的返回值(json.username)給 alert出來(lái)
$('#result').html("姓名:" + json.username + "<br/>密碼:" + json.password); //把php中的返回值顯示在預(yù)定義的result定位符位置
}
});
//$.post()方式:
$('#test_post').mousedown(function (){
$.post(
'login.php',
{
username:$('#username').val(),
password:$('#password').val()
},
function (data) //回傳函數(shù)
{
var myjson='';
eval('myjson=' + data + ';');
$('#result').html("姓名1:" + myjson.username + "<br/>密碼1:" + myjson.password);
}
);
});
//$.get()方式:
$('#test_get').mousedown(function ()
{
$.get(
'login.php',
{
username:$('#username').val(),
password:$('#password').val()
},
function(data) //回傳函數(shù)
{
var myjson='';
eval("myjson=" + data + ";");
$('#result').html("姓名2:" + myjson.username + "<br/>密碼2:" + myjson.password);
}
);
});
}
</script>
<body>
<div id="result" style="background:orange;border:1px solid red;width:300px;height:200px;"></div>
<form id="formtest" action="" method="post">
<p><span>輸入姓名:</span><input type="text" name="username" id="username" /></p>
<p><span>輸入密碼:</span><input type="text" name="password" id="password" /></p>
</form>
<button id="button_login">ajax提交</button>
<button id="test_post">post提交</button>
<button id="test_get">get提交</button>
</body>
</html>
php代碼
<?php
echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$_REQUEST['password']));
?>
使用jquery ajax優(yōu)點(diǎn)與持缺點(diǎn)分析
優(yōu)點(diǎn)
小,壓縮后代碼只有20多k(無(wú)壓縮代碼94k)。
Selector和DOM操作的方便:jQuery的Selector與mootools的Element.Selectors.js比較,CSS Selector, XPath Selector(1.2后已刪除)
Chaining:總是返回一個(gè)jQuery對(duì)象,可以連續(xù)操作。
文檔的完整,易用性(每個(gè)API都有完整的例子,這是其它框架現(xiàn)在不能比的),而且網(wǎng)上還有很多其它的文檔,書籍。
應(yīng)用的廣泛,包括google code也使用了jQuery。
使用jQuery的站點(diǎn):http://docs.jquery.com/Sites_Using_jQuery
核心的開發(fā)團(tuán)隊(duì)和核心人員:John Resig等。
簡(jiǎn)潔和簡(jiǎn)短的語(yǔ)法,容易記。
可擴(kuò)展性:有大量用戶開發(fā)的插件可供使用(http://jquery.com/plugins/)
jQuery UI(http://jquery.com/plugins/,基于jQuery,但和核心的jQuery是獨(dú)立的),不斷發(fā)展中。
友好和活躍的社區(qū):google groups:http://docs.jquery.com/Discussion
事件處理有很多方便的方法,如click,而不是單一的addEvent之類的。
缺點(diǎn)
由于設(shè)計(jì)思想是追求高效和簡(jiǎn)潔,沒(méi)有面向?qū)ο蟮臄U(kuò)展。設(shè)計(jì)思路和Mootools不一樣。
CSS Selector的速度稍微有些慢(但是現(xiàn)在速度已經(jīng)大幅提高)
實(shí)例1
復(fù)制代碼 代碼如下:
$.ajax({
type:'post',//可選get
url:'action.php',//這里是接收數(shù)據(jù)的PHP程序
data:'data='dsa',//傳給PHP的數(shù)據(jù),多個(gè)參數(shù)用&連接
dataType:'text',//服務(wù)器返回的數(shù)據(jù)類型 可選XML ,Json jsonp script html text等
success:function(msg){
//這里是ajax提交成功后,PHP程序返回的數(shù)據(jù)處理函數(shù)。msg是返回的數(shù)據(jù),數(shù)據(jù)類型在dataType參數(shù)里定義!
},
error:function(){
ajax提交失敗的處理函數(shù)!
}
})
PHP action.php
復(fù)制代碼 代碼如下:
<?php
echo 'www.dhdzp.com!';
?>
下面看個(gè)jquery ajax用戶無(wú)刷新登錄實(shí)例
實(shí)例2
復(fù)制代碼 代碼如下:
<html>
<head>
<title>jQuery Ajax 實(shí)例演示</title>
</head>
<script src="./js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){//這個(gè)就是jQueryready ,它就像C語(yǔ)言的main 所有操作包含在它里面
$("#button_login").mousedown(function(){
login(); //點(diǎn)擊ID為"button_login"的按鈕后觸發(fā)函數(shù) login();
});
});
function login(){ //函數(shù) login();
var username = $("#username").val();//取框中的用戶名
var password = $("#password").val();//取框中的密碼
$.ajax({ //一個(gè)Ajax過(guò)程
type: "post", //以post方式與后臺(tái)溝通
url : "login.php", //與此php頁(yè)面溝通
dataType:'json',//從php返回的值以 JSON方式 解釋
data: 'username='+username+'&password='+password, //發(fā)給php的數(shù)據(jù)有兩項(xiàng),分別是上面?zhèn)鱽?lái)的u和p
success: function(json){//如果調(diào)用php成功
//alert(json.username+'n'+json.password); //把php中的返回值(json.username)給 alert出來(lái)
$('#result').html("姓名:" + json.username + "<br/>密碼:" + json.password); //把php中的返回值顯示在預(yù)定義的result定位符位置
}
});
//$.post()方式:
$('#test_post').mousedown(function (){
$.post(
'login.php',
{
username:$('#username').val(),
password:$('#password').val()
},
function (data) //回傳函數(shù)
{
var myjson='';
eval('myjson=' + data + ';');
$('#result').html("姓名1:" + myjson.username + "<br/>密碼1:" + myjson.password);
}
);
});
//$.get()方式:
$('#test_get').mousedown(function ()
{
$.get(
'login.php',
{
username:$('#username').val(),
password:$('#password').val()
},
function(data) //回傳函數(shù)
{
var myjson='';
eval("myjson=" + data + ";");
$('#result').html("姓名2:" + myjson.username + "<br/>密碼2:" + myjson.password);
}
);
});
}
</script>
<body>
<div id="result" style="background:orange;border:1px solid red;width:300px;height:200px;"></div>
<form id="formtest" action="" method="post">
<p><span>輸入姓名:</span><input type="text" name="username" id="username" /></p>
<p><span>輸入密碼:</span><input type="text" name="password" id="password" /></p>
</form>
<button id="button_login">ajax提交</button>
<button id="test_post">post提交</button>
<button id="test_get">get提交</button>
</body>
</html>
php代碼
復(fù)制代碼 代碼如下:
<?php
echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$_REQUEST['password']));
?>
使用jquery ajax優(yōu)點(diǎn)與持缺點(diǎn)分析
優(yōu)點(diǎn)
小,壓縮后代碼只有20多k(無(wú)壓縮代碼94k)。
Selector和DOM操作的方便:jQuery的Selector與mootools的Element.Selectors.js比較,CSS Selector, XPath Selector(1.2后已刪除)
Chaining:總是返回一個(gè)jQuery對(duì)象,可以連續(xù)操作。
文檔的完整,易用性(每個(gè)API都有完整的例子,這是其它框架現(xiàn)在不能比的),而且網(wǎng)上還有很多其它的文檔,書籍。
應(yīng)用的廣泛,包括google code也使用了jQuery。
使用jQuery的站點(diǎn):http://docs.jquery.com/Sites_Using_jQuery
核心的開發(fā)團(tuán)隊(duì)和核心人員:John Resig等。
簡(jiǎn)潔和簡(jiǎn)短的語(yǔ)法,容易記。
可擴(kuò)展性:有大量用戶開發(fā)的插件可供使用(http://jquery.com/plugins/)
jQuery UI(http://jquery.com/plugins/,基于jQuery,但和核心的jQuery是獨(dú)立的),不斷發(fā)展中。
友好和活躍的社區(qū):google groups:http://docs.jquery.com/Discussion
事件處理有很多方便的方法,如click,而不是單一的addEvent之類的。
缺點(diǎn)
由于設(shè)計(jì)思想是追求高效和簡(jiǎn)潔,沒(méi)有面向?qū)ο蟮臄U(kuò)展。設(shè)計(jì)思路和Mootools不一樣。
CSS Selector的速度稍微有些慢(但是現(xiàn)在速度已經(jīng)大幅提高)
您可能感興趣的文章:
- jQuery+Ajax用戶登錄功能的實(shí)現(xiàn)
- jquery 彈出登錄窗口實(shí)現(xiàn)代碼
- PHP+jQuery+Ajax實(shí)現(xiàn)用戶登錄與退出
- jquery ajax 登錄驗(yàn)證實(shí)現(xiàn)代碼
- 基于Jquery+div+css實(shí)現(xiàn)彈出登錄窗口(代碼超簡(jiǎn)單)
- JQuery記住用戶名密碼實(shí)現(xiàn)下次自動(dòng)登錄功能
- 使用Jquery打造最佳用戶體驗(yàn)的登錄頁(yè)面的實(shí)現(xiàn)代碼
- javascript和jquery實(shí)現(xiàn)用戶登錄驗(yàn)證
- jQuery實(shí)現(xiàn)彈出窗口中切換登錄與注冊(cè)表單
- jQuery實(shí)現(xiàn)簡(jiǎn)單登錄條件判斷
相關(guān)文章
jQuery Raty星級(jí)評(píng)分插件使用方法實(shí)例分析
這篇文章主要介紹了jQuery Raty星級(jí)評(píng)分插件使用方法,結(jié)合實(shí)例形式分析了jquery Raty星級(jí)評(píng)分插件的下載、調(diào)用、基本屬性及相關(guān)使用技巧,需要的朋友可以參考下2019-11-11
Jquery數(shù)字上下滾動(dòng)動(dòng)態(tài)切換插件
有時(shí)我們需要?jiǎng)討B(tài)的展示訪問(wèn)次數(shù)、下載次數(shù)等效果,我們可以借助jQuery結(jié)合后臺(tái)php實(shí)現(xiàn)一個(gè)滾動(dòng)的數(shù)字展示效果。2015-08-08
jQuery獲取復(fù)選框選中的當(dāng)前行的某個(gè)字段的值
這篇文章主要介紹了jQuery獲取復(fù)選框選中的當(dāng)前行的某個(gè)字段的值,需要的朋友可以參考下2017-09-09
Json2Template.js 基于jquery的插件 綁定JavaScript對(duì)象到Html模板中
Json2Template.js是一個(gè)Jquery插件, 用來(lái)綁定JavaScript對(duì)象到Html模板中2011-10-10
jQuery(1.6.3) 中css方法對(duì)浮動(dòng)的實(shí)現(xiàn)缺陷分析
JavaScript中設(shè)置元素的浮動(dòng)屬性(float),標(biāo)準(zhǔn)瀏覽器使用cssFloat,IE舊版本使用styleFloat。2011-09-09
jQuery實(shí)現(xiàn)頁(yè)面倒計(jì)時(shí)并刷新效果
頁(yè)面倒計(jì)時(shí)小編在很多網(wǎng)站都有這樣的需求,今天小編給大家分享一段jq代碼實(shí)現(xiàn)頁(yè)面倒計(jì)時(shí)并刷新效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的的朋友參考下2017-03-03
jQuery實(shí)現(xiàn)仿京東防抖動(dòng)菜單效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)仿京東防抖動(dòng)菜單效果,結(jié)合實(shí)例形式分析了jQuery事件響應(yīng)及頁(yè)面屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-07-07

