jQuery ajax通过POST发送数据:正在发送错误的数据

时间:2013-01-24 11:27:37

标签: jquery ajax post

点击按钮发送数据的位:

$.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>coformation/appoint/",
    data: {
            "director":director.attr('checked'),
            "shareholder":shareholder.attr('checked'),
            "secretary":secretary.attr('checked'),
            "firstname":firstname.attr('value'),
            "lastname":lastname.attr('value'),
            "birthday":birthday.attr('value'),
            "birthmonth":birthmonth.attr('value'),
            "birthyear":birthyear.attr('value'),
            "occupation":occupation.attr('value'),
            "nationality":nationality.attr('value'),
            "security1":security1.attr('value'),
            "securityletters1":securityletters1.attr('value'),
            "security2":security2.attr('value'),
            "securityletters2":securityletters2.attr('value'),
            "security3":security3.attr('value'),
            "securityletters3":securityletters3.attr('value'),
            "residentialaddress":residentialaddr.text(),
            "serviceaddress":serviceaddr.text()
          },
    success: function(response) { 
            alert(response.message);
    },
    dataType: "json"
});

但是当我在目标网址中使用var_dump($ _ POST)时,似乎此过程只发出2个字段:

array (size=2)  'residentialaddress' => string 'XXX' (length=34)  'serviceaddress' => string '' (length=0)

可能是什么问题?

3 个答案:

答案 0 :(得分:2)

你在那里张贴属性attr() ..请通过链接查看attr实际上是做什么..

你甚至可以尝试

console.log(director.attr('checked'));  //which you 'll notice that this is not you want

 "director":director.attr('checked'),....

这发送是真还是假..

应该是

 "director":director.val(),"shareholder":shareholder.val(),....

val()为复选框或收音机提供已选中元素的值。文本如果是文本框....我认为这就是你想要的

答案 1 :(得分:1)

当然只会发送其中两个:

data: {
        "director":director.attr('checked').val(),
        "shareholder":shareholder.attr('checked').val(),
        "secretary":secretary.attr('checked').val(),
        "firstname":firstname.val(),
        "lastname":lastname.val(),
        "birthday":birthday.val(),
        "birthmonth":birthmonth.val(),
        "birthyear":birthyear.val(),
        "occupation":occupation.val(),
        "nationality":nationality.val(),
        "security1":security1.val(),
        "securityletters1":securityletters1.val(),
        "security2":security2.val(),
        "securityletters2":securityletters2.val(),
        "security3":security3.val(),
        "securityletters3":securityletters3.val(),
        "residentialaddress":residentialaddr.text(), //<------it gets the text
        "serviceaddress":serviceaddr.text()          //<------it gets the text
      },

您的所有其他项目只是创建不发送值.val()的对象,因此您应该将.val()追加到其他所有项目之后。

答案 2 :(得分:0)

如果数据对象中的键值未定义,则不会发送 例如:

data: {
  'first': 'text',
  'second': undefined
}

第二次不会被发送