使用AJAX提交表单laravel
我试图使用AJAX技术添加评论,但我有一个错误: Failed to load resource: http://localhost:8888/blog/public/comment/add the server responded with a status of 500 (Internal Server Error)
这里是我的代码:查看:
{{ Form::open(array('method'=>'post','class'=> 'col-md-6','url' => '/comment/add', 'id'=>'comment')) }} {{Form::label('name', 'Imię')}} {{Form::text('username', null, array('class'=>'form-control', 'id'=>'name', 'name'=>'name'))}} {{Form::label('message', 'Wiadomość')}} {{Form::textarea('message', null, array('class'=>'form-control', 'id'=>'message', 'name'=>'message', 'rows'=>'5'))}} {{Form::submit('Wyślij', array('name'=>'submit', 'class'=>'btn btn-orange'))}} {{ Form::close() }}
控制器:
public function addComment() { $this->layout = null; //check if its our form if(Request::ajax()){ $name = Input::get( 'name' ); $content = Input::get( 'message' ); $comment = new Comment(); $comment->author = $name; $comment->comment_content = $content; $comment->save(); $postComment = new CommentPost(); $postComment->post_id = Input::get('post_id'); $postComment->comment_id = Comment::max('id'); $postComment->save(); $response = array( 'status' => 'success', 'msg' => 'Setting created successfully', ); return 'yea'; }else{ return 'no'; } }
AJAX:
jQuery( document ).ready( function( $ ) { $( '#comment' ).on( 'submit', function(e) { e.preventDefault(); var name = $(this).find('input[name=name]').val(); $.ajax({ type: "POST", url: host+'/comment/add', }).done(function( msg ) { alert( msg ); }); }); });
最后一条路线:
Route::post('comment/add', 'CommentController@addComment');
任何人都知道问题出在哪里以及为什么我不能提交表格?
您没有发布任何数据,
$.ajax({ type: "POST", url: host+'/comment/add', }).done(function( msg ) { alert( msg ); });
您得到的错误是DB中的列不能为空。
尝试将您的ajax调用更改为:
$.ajax({ type: "POST", url: host+'/comment/add', data: { name:name, message:message, post_id:postid }, success: function( msg ) { alert( msg ); } });
改变这个
var name = $(this).find('input[name=name]').val();
至
var name = $('#name').val();
并获取消息和postID:
var message = $('#message').val(); var postid = $('#post_id').val();
完成ajax块:
$('#comment').on('submit', function(e) { e.preventDefault(); var name = $('#name').val(); var message = $('#message').val(); var postid = $('#post_id').val(); $.ajax({ type: "POST", url: host+'/comment/add', data: {name:name, message:message, post_id:postid} success: function( msg ) { alert( msg ); } }); });
最后,在隐藏字段中添加ID:
从Laravel控制器发回数据,例如。
// ........ $response = array( 'status' => 'success', 'msg' => 'Setting created successfully', ); return Response::json($response); // <<<<<<<<< see this line }else{ return 'no'; } }
这会将您的响应中的数据发送回您的ajax请求。
然后,改变你的ajax成功函数:
// ....... success: function( msg ) { $("body").append(""+msg+""); } // ..........
您现在将看到在创建了一个新的div,包括创建的响应。 如果要显示新创建的post,只需将其创建为ajax响应并将其附加到页面中的任何元素即可。
只修改baao答案的ajax块。 您可以将数据作为序列化传递。
$('#comment').on('submit', function(e) { e.preventDefault(); $.ajax({ type: "POST", url: host+'/comment/add', data: $(this).serialize(), success: function(msg) { alert(msg); } }); });
可以使用serialize()
函数传递表单的所有字段值。