jquery.post中的data


需求:

  使用jquery的post傳遞參數

 

語法:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
jQuery.post(url,data,success(data, textStatus, jqXHR),dataType)
參數 描述
url 必需。規定把請求發送到哪個 URL。
data 可選。映射或字符串值。規定連同請求發送到服務器的數據。
success(data, textStatus, jqXHR) 可選。請求成功時執行的回調函數。
dataType

可選。規定預期的服務器響應的數據類型。

默認執行智能判斷(xml、json、script 或 html)。

 

使用:

  1、平時我的習慣用法

var $postUrl = "index.php?m=cms";
var $data = {"act":"del","moduleid":"95"};
var $type = "json";
$.post($postUrl,$data,$type);

  firebug查看參數

  2、遇到問題:構造data,后台使用post不能獲取數據,使用firebug查看,post過去的參數為json對象

var $postUrl = "index.php?m=cms";
var $data = "{";
var $type = "json";
$("#obj").siblings().each(function(i){
      if(i==0){
         $data += $(this).attr("name")+":"+$(this).val();
      }else{
         $data += ","+$(this).attr("name")+":"+$(this).val();
      }
});
$data += "}";
$.post($postUrl,$data,$type);

  firebug查看參數

  還未完全明白json數據格式定義,於是采用其他方案代替

var $postUrl = "index.php?m=cms";
var $data = "";
var $type = "json";
$("#obj").siblings().each(function(i){
         $data += $(this).attr("name")+"="+$(this).val()+"&";
});
$.post($postUrl,$data,$type);

 

參考:

w3school

jquery.com

 

注:

本文中為部分示例代碼,不能直接運行

本人還屬菜鳥,有什么不對的地方,請求指教


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM