需求:

  使用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查看参数

jquery.post中的data

  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查看参数

jquery.post中的data

  还未完全明白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

 

注:

本文中为部分示例代码,不能直接运行

本人还属菜鸟,有什么不对的地方,请求指教

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2021-07-24
  • 2022-02-14
  • 2021-05-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-02
  • 2021-07-20
  • 2022-12-23
  • 2021-05-16
  • 2022-12-23
相关资源
相似解决方案