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,等等)
這是一個簡單的 POST 請求功能以取代復(fù)雜 $.ajax 。請求成功時可調(diào)用回調(diào)函數(shù)。如果需要在出錯時執(zhí)行函數(shù),請使用 $.ajax。示例代碼:
Ajax.aspx:
Response.ContentType?=?"application/json";Response.Write("{result:?'"?+?Request["Name"]?+?",你好!(這消息來自服務(wù)器)'}");
jQuery 代碼:
$.post("Ajax.aspx",?{?Action:?"post",?Name:?"lulu"?},?????
???function?(data,?textStatus){????????
????//?data?可以是?xmlDoc,?jsonObj,?html,?text,?等等.??
????//this;
????//?這個Ajax請求的選項配置信息,請參考jQuery.get()說到的this??
???alert(data.result);????????},?"json");
點(diǎn)擊提交:
這里設(shè)置了請求的格式為"json":
$.ajax()這個是jQuery 的底層 AJAX 實現(xiàn)。簡單易用的高層實現(xiàn)見 $.get, $.post 等。
這里有幾個Ajax事件參數(shù):
beforeSend ,
success ,
complete ,error 。我們可以定義這些事件來很好的處理我們的每一次的Ajax請求。
?
$.ajax({
url:?'stat.php',
type:?'POST',
data:{Name:"keyun"},
dataType:?'html',
timeout:?1000,
error:?function(){alert('Error?loading?PHP?document');},
success:?function(result){alert(result);}
});
?
//add by Q at 2008.11.25
今天遇到一個jquery的post的小問題
因為要批量刪除,所以開始用循環(huán)的post到那個url,然后刷新本頁
這就出現(xiàn)問題了
?
$("input[@name='qa_checkbox']").each(function()
{
????if($(this).attr('checked')?==?undefined)
????{
????????????????
????}
????else
????{
????????$.post(url,{Action:"POST"},function(data){alert(data);window.location.reload();},?"text");
????????????????
????}
})
這么用的話 只能刪除第一條數(shù)據(jù);
?
$("input[@name='qa_checkbox']").each(function()
{
????if($(this).attr('checked')?==?undefined)
????{
????????????????
????}
????else
????{
????????$.post(url+$(this).val(),{Action:"POST"},function(data){alert(data);},?"text");
????????????????
????}
})
window.location.reload();
?
這樣用的話,雖然可以刪除,也能刷新本頁,貌似reload是在post的function之前運(yùn)行,但是post會報錯,其中原因有待研究;
最終想了折中的辦法?
$("input[@name='qa_checkbox']").each(function()
????????{
????????????if($(this).attr('checked')?==?undefined)
????????????{
????????????????
????????????}
????????????else
????????????{
????????????????url?=?url?+?$(this).val()?+?'_';
????????????????
????????????}
????????})
????????$.post(url,{Action:"POST"},function(data){alert(data);window.location.reload();},?"text");
????}
?
把要刪除的id連成字符串,用一次post處理,把reload放在post的function里 就沒有問題了
posted on 2009-10-28 14:17
becket_zheng 閱讀(451)
評論(0) 編輯 收藏 所屬分類:
網(wǎng)頁web前端技術(shù)