jquery中$.post()方法的簡單實例
在jqery中有這樣一個方法,$.post()下面就這個方法做一個簡單的實例:
jQuery.post( url, [data], [callback], [type] ) :
使用POST方式來進(jìn)行異步請求
參數(shù):
url (String) : 發(fā)送請求的URL地址.
data (Map) : (可選) 要發(fā)送給服務(wù)器的數(shù)據(jù),以 Key/value 的鍵值對形式表示。
callback (Function) : (可選) 載入成功時回調(diào)函數(shù)(只有當(dāng)Response的返回狀態(tài)是success才是調(diào)用該方法)。
type (String) : (可選)官方的說明是:Type of data to be sent。其實應(yī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)點擊按鈕時,注意按鈕現(xiàn)在的類型是button.在不使用$.post()方法時,按鈕類型是submit,這樣submit提交form里的數(shù)據(jù),采用post方法傳遞到頁面action.php,這時在頁面action.php中就能接受到傳過來的數(shù)據(jù)。當(dāng)采用$.post方法時,我們在函數(shù)ajax_post()方法中其實就是使用了post的方法。(要引用jquery庫文件)
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結(jié)
- javascript jQuery $.post $.ajax用法
- jQuery get和post 方法傳值注意事項
- jQuery中ajax的post()方法用法實例
- jquery ajax post提交數(shù)據(jù)亂碼
- Jquery AJAX POST與GET之間的區(qū)別
- jquery post方式傳遞多個參數(shù)值后臺以數(shù)組的方式進(jìn)行接收
- jquery中g(shù)et,post和ajax方法的使用小結(jié)
- jquery向.ashx文件post中文亂碼問題的解決方法
- jQuery post數(shù)據(jù)至ashx實例詳解
相關(guān)文章
原生Ajax 和jQuery Ajax的區(qū)別示例分析
這篇文章主要介紹了原生Ajax 和Jq Ajax的區(qū)別示例分析,需要的朋友可以參考下2014-12-12
jQuery密碼強(qiáng)度檢測插件passwordStrength用法實例分析
這篇文章主要介紹了jQuery密碼強(qiáng)度檢測插件passwordStrength用法,以一個完整實例形式較為詳細(xì)的分析了passwordStrength插件針對密碼強(qiáng)度的檢測方法,需要的朋友可以參考下2015-10-10
jQuery leonaScroll 1.1 自定義滾動條插件(推薦)
這篇文章主要介紹了jQuery leonaScroll 1.1 自定義滾動條插件(推薦)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09

