【问题标题】:jQuery ajax sending data through POST : wrong data is being sentjQuery ajax 通过 POST 发送数据:正在发送错误的数据
【发布时间】:2013-01-24 11:27:37
【问题描述】:

单击按钮时发送数据的位:

$.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"
});

但是当我在目标 URL 中使用 var_dump($_POST) 时,这个过程似乎只发送了 2 个字段:

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

可能是什么问题?

【问题讨论】:

  • 尝试使用调试控制台(Firebug 或 Chrome 控制台)在浏览器中查看 POST 请求。
  • .attr('value') 只会返回在原始 html 中设置的值,并且只有 value 属性在原始 html, will not reflect user input. Use val()` 中用于表单控件

标签: jquery ajax post


【解决方案1】:

您正在那里发布属性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() 为您提供选中元素的值,如果复选框或单选.. 文本如果文本框.... 我认为这就是你想要的

【讨论】:

    【解决方案2】:

    当然只会发送其中两个:

    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()

    【讨论】:

      【解决方案3】:

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

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

      秒不会发送

      【讨论】:

        猜你喜欢
        • 2018-03-08
        • 1970-01-01
        • 2014-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多