带有 ajax 和 jquery serialize() 的 POST 请求丢失了值
POST request with ajax and jquery serialize() lost values
我正在尝试从发布请求中检索四个值,但我只得到其中的 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 函数是否有可能在点击事件上不执行?
相关文章:
- 当我在form_for中的text_field_tag中点击Enter时,如何禁止发出POST请求
- 为什么 AJAX POST 请求不起作用
- Ajax POST请求没有'我不了解PHP
- 防止双击执行两次jQuery post请求
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 如何使用Javascript获取POST请求填充的元素的值
- NodeJS中POST请求的请求体为null
- 在Fiddler JavaScript中识别POST请求
- DreamFactory REST API POST休息/用户/会话请求总是在IE9中返回错误
- 使用application/x-www-form-urlencoded使用node.js在post请求中发送数组
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 通过AJAX向同一页面发送POST请求,并使用$_POST获取值
- Ajax Post请求不起作用
- 如何在cloud9中向server.js发送post请求
- mootools表单json post请求
- 可以'无法在CasperJS中复制POST请求
- node.js请求POST数组“;第一个参数必须是字符串或缓冲区;
- PythonBottle:访问请求POST数据中的数组变量
- NodeJS请求POST图标
- Yii框架2.0 AJAX请求POST方法