標籤:style blog http color io os 使用 ar strong
jQuery.post( url, [data], [callback], [type] ) :使用POST方式來進行非同步請求
參數:
url (String) : 發送請求的URL地址.
data (Map) : (可選) 要發送給伺服器的資料,以 Key/value 的索引值對形式表示。
callback (Function) : (可選) 載入成功時回呼函數(只有當Response的返回狀態是success才是調用該方法)。
type (String) : (可選)官方的說明是:Type of data to be sent。其實應該為用戶端請求的類型(JSON,XML,等等)
這是一個簡單的 POST 請求功能以取代複雜 $.ajax 。請求成功時可調用回呼函數。如果需要在出錯時執行函數,請使用 $.ajax。範例程式碼:
Ajax.aspx:
Response.ContentType = "application/json";Response.Write("{result: ‘" + Request["Name"] + ",你好!(這訊息來自伺服器)‘}");
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");
點擊提交:
這裡設定了請求的格式為"json":
$.ajax()這個是jQuery 的底層 AJAX 實現。簡單易用的高層實現見 $.get, $.post 等。
這裡有幾個Ajax事件參數: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的小問題
因為要大量刪除,所以開始用迴圈的post到那個url,然後重新整理本頁
這就出現問題了
$("input[@name=‘qa_checkbox‘]").each(function()
{
if($(this).attr(‘checked‘) == undefined)
{
}
else
{
$.post(url,{Action:"POST"},function(data){alert(data);window.location.reload();}, "text");
}
})
這麼用的話 只能刪除第一條資料;
$("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之前運行,但是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裡 就沒有問題了
項目執行個體:
function clearSys() {
$.dialog.confirm(‘你確認刪除操作?‘, function(){
$.post("{:getUrl(‘ucentermsg/clearsys‘)}",function(data){
if (!data) {
Kshop.optFail();
return false;
}
if (data.status == 1){
setTimeout(‘window.location.href=window.location.href;‘,1000);
Kshop.optDone(data.info);
}else{
Kshop.optFail(data.info);
}
}, ‘json‘);
});
}
後台:
public function clearSys()
{
$msg = D(‘Message‘);
$result = $msg->saveAllFlags($this->user_info[‘uid‘], $msg::SYS_TO_UID);
if ($result) {
$this->ajaxReturn(‘‘, ‘刪除成功!‘, 1);
} else {
$this->ajaxReturn(‘‘, ‘刪除失敗!‘, 0);
}
}
protected function ajaxReturn($data,$info=‘‘,$status=1,$type=‘‘) {
$result = array();
$result[‘status‘] = $status;
$result[‘info‘] = $info;
$result[‘data‘] = $data;
//擴充ajax返回資料, 在Action中定義function ajaxAssign(&$result){} 方法 擴充ajax返回資料。
if(method_exists($this,"ajaxAssign"))
$this->ajaxAssign($result);
if(empty($type)) $type = C(‘DEFAULT_AJAX_RETURN‘);
if(strtoupper($type)==‘JSON‘) {
// 返回JSON資料格式到用戶端 包含狀態資訊
header("Content-Type:text/html; charset=utf-8");
exit(json_encode($result,JSON_HEX_TAG));
}elseif(strtoupper($type)==‘XML‘){
// 返回xml格式資料
header("Content-Type:text/xml; charset=utf-8");
exit(xml_encode($result));
}elseif(strtoupper($type)==‘EVAL‘){
// 返回可執行檔js指令碼
header("Content-Type:text/html; charset=utf-8");
exit($data);
}else{
// TODO 增加其它格式
}
}
jQuery $.post $.ajax用法