jquery中$.post()方法的簡(jiǎn)單實(shí)例
在jqery中有這樣一個(gè)方法,$.post()下面就這個(gè)方法做一個(gè)簡(jiǎn)單的實(shí)例:
jQuery.post( url, [data], [callback], [type] ) :
使用POST方式來進(jìn)行異步請(qǐng)求
參數(shù):
url (String) : 發(fā)送請(qǐng)求的URL地址.
data (Map) : (可選) 要發(fā)送給服務(wù)器的數(shù)據(jù),以 Key/value 的鍵值對(duì)形式表示。
callback (Function) : (可選) 載入成功時(shí)回調(diào)函數(shù)(只有當(dāng)Response的返回狀態(tài)是success才是調(diào)用該方法)。
type (String) : (可選)官方的說明是:Type of data to be sent。其實(shí)應(yīng)該為客戶端請(qǐng)求的類型(JSON,XML,等等)
1.html頁面(index.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
<script type="text/javascript" src=\'#\'" /jquery-1.3.2.js"></script>
<script language="javascript">
function checkemail(){
if($('#email').val() == ""){
$('#msg').html("please enter the email!");
$('#email').focus;
return false;
}
if($('#address').val() == ""){
$('#msg').html("please enter the address!");
$('#address').focus;
return false;
}
ajax_post();
}
function ajax_post(){
$.post("action.php",{email:$('#email').val(),address:$('#address').val()},
function(data){
//$('#msg').html("please enter the email!");
//alert(data);
$('#msg').html(data);
},
"text");//這里返回的類型有:json,html,xml,text
}
</script>
</head>
<body>
<form id="ajaxform" name="ajaxform" method="post" action="action.php">
<p>
email<input type="text" name="email" id="email"/>
</p>
<p>
address<input type="text" name="address" id="address"/>
</p>
<p id="msg"></p>
<p>
<input name="Submit" type="button" value="submit" onclick="return checkemail()"/>
</p>
</form>
</body>
</html>
2.php頁面(action.php)
<?php
$email = $_POST["email"];
$address = $_POST["address"];
//echo $email;
//echo $address;
echo "success";
?>
說明:當(dāng)點(diǎn)擊按鈕時(shí),注意按鈕現(xiàn)在的類型是button.在不使用$.post()方法時(shí),按鈕類型是submit,這樣submit提交form里的數(shù)據(jù),采用post方法傳遞到頁面action.php,這時(shí)在頁面action.php中就能接受到傳過來的數(shù)據(jù)。當(dāng)采用$.post方法時(shí),我們?cè)诤瘮?shù)ajax_post()方法中其實(shí)就是使用了post的方法。(要引用jquery庫文件)
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結(jié)
- javascript jQuery $.post $.ajax用法
- jQuery get和post 方法傳值注意事項(xiàng)
- jQuery中ajax的post()方法用法實(shí)例
- jquery ajax post提交數(shù)據(jù)亂碼
- Jquery AJAX POST與GET之間的區(qū)別
- jquery post方式傳遞多個(gè)參數(shù)值后臺(tái)以數(shù)組的方式進(jìn)行接收
- jquery中g(shù)et,post和ajax方法的使用小結(jié)
- jquery向.ashx文件post中文亂碼問題的解決方法
- jQuery post數(shù)據(jù)至ashx實(shí)例詳解
相關(guān)文章
原生Ajax 和jQuery Ajax的區(qū)別示例分析
這篇文章主要介紹了原生Ajax 和Jq Ajax的區(qū)別示例分析,需要的朋友可以參考下2014-12-12
jquery siblings獲取同輩元素用法實(shí)例分析
這篇文章主要介紹了jquery siblings獲取同輩元素用法,結(jié)合實(shí)例形式分析了jQuery使用siblings遍歷同級(jí)元素的相關(guān)技巧,需要的朋友可以參考下2016-07-07
jQuery密碼強(qiáng)度檢測(cè)插件passwordStrength用法實(shí)例分析
這篇文章主要介紹了jQuery密碼強(qiáng)度檢測(cè)插件passwordStrength用法,以一個(gè)完整實(shí)例形式較為詳細(xì)的分析了passwordStrength插件針對(duì)密碼強(qiáng)度的檢測(cè)方法,需要的朋友可以參考下2015-10-10
jQuery實(shí)現(xiàn)判斷控件是否顯示的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)判斷控件是否顯示的方法,涉及jQuery針對(duì)頁面元素屬性相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
jQuery leonaScroll 1.1 自定義滾動(dòng)條插件(推薦)
這篇文章主要介紹了jQuery leonaScroll 1.1 自定義滾動(dòng)條插件(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
jquery實(shí)現(xiàn)多級(jí)下拉菜單的實(shí)例代碼
多級(jí)菜單,理論上支持無限多的層級(jí),文件結(jié)構(gòu)非常簡(jiǎn)單的,以下是完整代碼,有需要的朋友可以參考一下2013-10-10
簡(jiǎn)單實(shí)現(xiàn)jQuery彈幕效果
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)現(xiàn)jQuery彈幕效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
JQuery實(shí)現(xiàn)的按鈕倒計(jì)時(shí)效果
這篇文章主要介紹了JQuery實(shí)現(xiàn)的按鈕倒計(jì)時(shí)效果,涉及jQuery結(jié)合時(shí)間函數(shù)動(dòng)態(tài)修改按鈕屬性的相關(guān)技巧,需要的朋友可以參考下2015-12-12

