【问题标题】:jQuery serialize adds additional field named '_' (underscore)jQuery 序列化添加名为 '_' 的附加字段(下划线)
【发布时间】:2018-06-06 14:25:05
【问题描述】:

我用:

$.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 时得到的结果:

【问题讨论】:

    标签: jquery json serialization


    【解决方案1】:

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

    这里是jQuery's docs for the cache property的解释:

    它通过将“_={timestamp}”附加到 GET 参数来工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      相关资源
      最近更新 更多