我完全不想尝试使用ajax而不是laravel处理表单提交以避免页面重新加载等.但它不起作用,我不知道为什么.我通过示例搜索了精彩的网页,但似乎没有什么对我有用.这是我能得到的最接近的.我的知识有限,但我的抱负很高.请花一点时间查看我的代码,因为我现在处于精神崩溃的边缘.
这是我的jquery:
$(document).ready(function() {
$('#ajax').submit(function(event){
$.ajax({
type: 'POST',
url: 'comments',
data: $('form#ajax').serialize(),
dataType: 'json',
})
.done(function(data) {
console.log(data);
});
event.preventDefault();
});
});
Run Code Online (Sandbox Code Playgroud)
这是我在刀片视图中的表单:
{{ Form::open(array('url'=>'comments', 'id' => 'ajax')) }}
{{ Form::hidden('parent_id', null) }}
{{ Form::hidden('author', Auth::user()->username) }}
{{ Form::textarea('content', null, array('placeholder' => 'Enter your comment here:', 'onfocus' => 'this.placeholder=""')) }}<br/>
{{ Form::submit('Comment', array('class'=>'submit')) }}
{{ Form::close() }}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
public function createComment() {
//fixa här så man inte kan kommentera tom kommentar
$validate = array('content' => 'required');
$validator = Validator::make(Input::all(), $validate); …Run Code Online (Sandbox Code Playgroud)