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-2025 CODEPRJ.COM