jQuery serialize添加了名为'_'(下划线)

时间:2018-06-06 14:25:06

标签: jquery json serialization

我用:

$.getJSON('addRecord.php', $('#addRecord').serialize())

通过AJAX将JSON数据传递给php函数。但是,除了表单的字段之外,我还得到一个名为“_”的字段,尽管我对表单没有任何作用。所有表单字段都有名称和ID,命令后面没有很多。

这是表格的代码:

<form id='addRecord'>
    <div class='form-group' id='coTypeIdD'>
        <label for='coTypeId'>Type</label>
        <input type='text' class='form-control' id='coTypeId' name='coTypeId' placeholder='Type'>
        <small id='coTypeIdM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coAccountIdD'>
        <label for='coAccountId'>Account</label>
        <input type='text' class='form-control' id='coAccountId' name='coAccountId' placeholder='Account'>
        <small id='coAccountIdM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coIsPrimaryD'>
        <label for='coIsPrimary'>Primary?</label>
        <input type='text' class='form-control' id='coIsPrimary' name='coIsPrimary' placeholder='Primary?'>
        <small id='coIsPrimaryM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coDescriptionD'>
        <label for='coDescription'>Description</label>
        <input type='text' class='form-control' id='coDescription' name='coDescription' placeholder='Description'>
        <small id='coDescriptionM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coUserD'>
        <label for='coUser'>User</label>
        <input type='text' class='form-control' id='coUser' name='coUser' placeholder='User'>
        <small id='coUserM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coSalutationD'>
        <label for='coSalutation'>Salutation</label>
        <input type='text' class='form-control' id='coSalutation' name='coSalutation' placeholder='Salutation'>
        <small id='coSalutationM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coFirstNameD'>
        <label for='coFirstName'>First Name</label>
        <input type='text' class='form-control' id='coFirstName' name='coFirstName' placeholder='First Name'>
        <small id='coFirstNameM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coLastNameD'>
        <label for='coLastName'>Last Name</label>
        <input type='text' class='form-control' id='coLastName' name='coLastName' placeholder='Last Name'>
        <small id='coLastNameM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coTitleD'>
        <label for='coTitle'>Title</label>
        <input type='text' class='form-control' id='coTitle' name='coTitle' placeholder='Title'>
        <small id='coTitleM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coDepartmentIdD'>
        <label for='coDepartmentId'>Department</label>
        <input type='text' class='form-control' id='coDepartmentId' name='coDepartmentId' placeholder='Department'>
        <small id='coDepartmentIdM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coPreferredCommunicationD'>
        <label for='coPreferredCommunication'>Preferred Communication</label>
        <input type='text' class='form-control' id='coPreferredCommunication' name='coPreferredCommunication' placeholder='Preferred Communication'>
        <small id='coPreferredCommunicationM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coPhoneD'>
        <label for='coPhone'>Extension</label>
        <input type='text' class='form-control' id='coPhone' name='coPhone' placeholder='Extension'>
        <small id='coPhoneM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coEmailD'>
        <label for='coEmail'>Email</label>
        <input type='text' class='form-control' id='coEmail' name='coEmail' placeholder='Email'>
        <small id='coEmailM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coMobileD'>
        <label for='coMobile'>Mobile </label>
        <input type='text' class='form-control' id='coMobile' name='coMobile' placeholder='Mobile '>
        <small id='coMobileM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coSkypeD'>
        <label for='coSkype'>coSkype</label>
        <input type='text' class='form-control' id='coSkype' name='coSkype' placeholder='coSkype'>
        <small id='coSkypeM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coFaxD'>
        <label for='coFax'>coFax</label>
        <input type='text' class='form-control' id='coFax' name='coFax' placeholder='coFax'>
        <small id='coFaxM' class='text-danger'></small>
    </div>
    <div class='form-group' id='coPhone2D'>
        <label for='coPhone2'>Alternate Phone</label>
        <input type='text' class='form-control' id='coPhone2' name='coPhone2' placeholder='Alternate Phone'>
        <small id='coPhone2M' class='text-danger'></small>
    </div>
    <div class='form-group' id='coBirthdateD'>
        <label for='coBirthdate'>Birthday</label>
        <input type='text' class='form-control' id='coBirthdate' name='coBirthdate' placeholder='Birthday'>
        <small id='coBirthdateM' class='text-danger'></small>
    </div>
</form>

这是我在PHP中转储$_GET时得到的结果:

dump of $_GET

1 个答案:

答案 0 :(得分:1)

jQuery会自动添加_字段以防止缓存数据。 $.getJSON()方法已预先配置,因此您无法更改其行为,但如果要禁用缓存,则可以使用$.ajax()方法。只需将其cache属性设置为false

以下是jQuery's docs for the cache property的解释:

  

通过追加&#34; _ = {timestamp}&#34;到GET参数。