【发布时间】:2015-07-18 11:52:03
【问题描述】:
现在我接到了要发布电子邮件的 Ajax 调用,但我仍在努力尝试检索它、验证它并将其插入到我的数据库中。
这是我得到的:
控制器
public function postSubscribe() {
if(Request::ajax()) {
$data = Input::all();
}
dd(json_decode($data));
// Validation
$validator = Validator::make( Input::only('subscribe_email'),
array(
'subscribe_email' => 'email|unique:subscribes,email',
)
);
if ($validator->fails()) {
return Redirect::to('/#footer')
->with('subscribe_error','This email is already subscribed to us.')
->withErrors($validator)->withInput();
}else{
$subscribe = new Subscribe;
$subscribe->email = Input::get('subscribe_email');
$subscribe->save();
return Redirect::to('/thank-you');
}
}
路线
Route::post('/subscribe','SubscribeController@postSubscribe');
既然是发帖请求,我在哪里可以看到我的 dd(json_decode($data)); ?我做对了吗?请纠正我。
表格
{!! Form::open(array('url' => '/subscribe', 'class' => 'subscribe-form', 'role' =>'form')) !!}
<div class="form-group col-lg-7 col-md-8 col-sm-8 col-lg-offset-1">
<label class="sr-only" for="mce-EMAIL">Email address</label>
<input type="email" name="subscribe_email" class="form-control" id="mce-EMAIL" placeholder="Enter email" required>
<!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;"><input type="text" name="b_168a366a98d3248fbc35c0b67_73d49e0d23" value=""></div>
</div>
<div class="form-group col-lg-3 col-md-4 col-sm-4">
<button type="button" name="subscribe" class="btn btn-primary btn-block" id="subscribe">Subscribe</button>
</div>
{!! Form::close() !!}
阿贾克斯
<script type="text/javascript">
$(document).ready(function(){
$('#subscribe').click(function(e){
e.preventDefault();
$.ajax({
url: '/subscribe',
type: "post",
data: {'subscribe_email':$('input[name=subscribe_email]').val(), '_token': $('input[name=_token]').val()},
dataType: 'JSON',
success: function (data) {
console.log(data);
} // this is good
});
});
});
</script>
发布数据成功
问题
我以后如何自己调试类似的东西? 在 PHP Laravel 中从 Ajax 调用中检索数据最实用的方法是什么?
【问题讨论】:
-
您发布了标题的屏幕截图,将标签切换为响应或预览。此外,您不必解码 $data
-
你是对的!我不应该解码它。另外,在将其更改为编码后,它可以工作,并将选项卡更改为响应,我看到了我现在发布的内容。
-
至于如何调试这样的东西,找到一个 IDE,它允许您发出 Web 请求并逐行执行您的代码。当您遇到问题时,它可以为您节省大量时间。
-
您对 Sublime Text 有什么了解吗?我现在使用 sublime text 3。
-
他们确实有一个 xdebug 插件,但我上次尝试过,它似乎并没有那么好用。与我一起工作的一些人使用 NetBeans,并声称它非常适合单步执行代码并且是免费的。
标签: php ajax laravel laravel-5