带有 ajax 和 jquery serialize() 的 POST 请求丢失了值

POST request with ajax and jquery serialize() lost values

本文关键字:请求 POST ajax jquery serialize 带有      更新时间:2023-09-26

我正在尝试从发布请求中检索四个值,但我只得到其中的 2 个。该表单包含ID,步骤,名称和电子邮件,但我仅从隐藏的输入中获取。而不是用户填写的那些。我认为这可能是jquery serialize()但我不确定。

我尝试将输入更改为隐藏,然后添加值="某物"并且它有效。为什么它适用于常规文本?

发布请求的结果:

[position_id] => 229
[step] => 1
[name] => 
[email] => 

窗体如下所示:

<form id="referral-form" action="#" method="post">
    <input type="hidden" name="position_id" value="{{$position->id}}" />
    <input type="hidden" name="step" value="1" />
    <div class="form-group">
        <input name="name" class="form-control" type="text" id="name" required/>
    </div>
    <div class="form-group">
        <input name="email" class="form-control" type="text" id="email" required />
    </div>
    <div id="legal">
        <span class="loader-button"></span>
        <div class="button submit"></div>
    </div>
</form>

我有一个javascript方法,如果用户点击.button.submit就会启动

onRef: function(e) {
    e.preventDefault();
    var $form = $("#referral-form");
    if(!$form.hasClass("ajax"))
    {
        $form.addClass("ajax");
        $form.find('.error').css('display', 'none');
        var req = $.post('/reff/ref', $form.serialize());
        req.done(function(res) {
            $form.removeClass("ajax");
            if(res.success) {
                //do somthing
            }
            else {
                methods.printErrors(res.errors, $form);
                mixpanel.track('onReferralValidationFailure', {
                    errors: res.errors,
                    positionId: exported.position.id,
                    companyId: exported.position.company_id
                });
            }
        });
        req.fail(function() {
            $form.removeClass("ajax");
            mixpanel.track('onReferralUnknownError', {
                positionId: exported.position.id,
                companyId: exported.position.company_id
            });
        });
    }
},

当我尝试在 php 的/reff/ref 函数中检索帖子数据时,我只得到这些数据。

[2016-07-07 11:58:39] local.INFO: Array
(
    [position_id] => 229
    [step] => 1
    [name] => 
    [email] => 
)

这是 reff/ref 中的函数:

 public function refer() {
    $positionId = Input::get("position_id");
    Language::setLanguageByPositionId($positionId);
    if(Input::get("step") == 1) {
        $validator = new ReferralStepOneValidator(App::make('validator'));
        $validator->with(Input::all());
        Log::info(print_r(Input::all(), true));
        if($validator->passes()) {
            $input = Input::all();

            $referral = Referral::createReferralFromInput($input);
            return Response::json(array(
                'success' => true,
                'reference' => $referral->reference,
            ));
        }
        else {
            return Response::json(array(
                'success' => false,
                'errors' => $validator->errors()->toArray(),
            ));
        }
    }

抱歉,我还无法发表评论。我正在关注这个问题并尝试了一些事情。您可以在发布前放置控制台日志吗?

<script>
$('#referral-form').click(function (e) {
    e.preventDefault();
    var $form = $("#referral-form");

    if(!$form.hasClass("ajax"))
    {
        $form.addClass("ajax");
        $form.find('.error').css('display', 'none');
        console.log($form.serialize());
        var req = $.post('/reff/ref', $form.serialize());
        req.done(function(res) {
            $form.removeClass("ajax");
            if(res.success) {
                //do somthing
            }
            else {
                methods.printErrors(res.errors, $form);
                mixpanel.track('onReferralValidationFailure', {
                    errors: res.errors,
                    positionId: exported.position.id,
                    companyId: exported.position.company_id
                });
            }
        });
        req.fail(function() {
            $form.removeClass("ajax");
            mixpanel.track('onReferralUnknownError', {
                positionId: exported.position.id,
                companyId: exported.position.company_id
            });
        });
    }
});
</script>

另外,您的 js 函数是否有可能在点击事件上不执行?