由ajax评论模块不在laravel 5中工作

时间:2017-04-15 08:02:15

标签: ajax forms laravel-5

我正在通过ajax在我的项目中评论模块。我收到此错误

<form action="{{route('comments.store')}}" method="post">
   {{  csrf_field() }}
   <div class="col-md-11 col-sm-11">
      <div class="form-group">
          <textarea name="comment-msg" id="comment-name" cols="30" rows="1" class="form-control" placeholder="comment here..."></textarea>
          <input  type="hidden" id="eventID" name="eventID" value="<?php echo $eventData->id; ?>">
          <input  type="hidden" id="userID" name="userID" value="<?php echo Auth::user()->id; ?>">
       </div>
    </div>
    <div class="col-md-12">
     <button type="submit" id="submit-comment" class="btn-primary pull-right">Comment</button>
    </div>
   </form>

并且数据未发布。我做错了什么?我的路线是资源路线,我想在不刷新页面的情况下显示它。

表格

<script>
    $.ajaxSetup({
        headers: {'X-CSRF-Token': $('meta[name=_token]').attr('content')}
    });

        $( '#submit-comment' ).click(function() {
            var formData = {
                'message'              : $('#comment-msg').val(),
                'eventID'             : $('#eventID').val(),
                'userID'             : $('#userID').val(),
            };


            $.ajax({
                        type        : 'POST',
                        url         : '{{route('comments.store')}}',
                        data        : formData,
                        dataType    : 'json', 
                        encode          : true,
                        success: function (response) {
                          console.log(response);
                        },
                        error: function(xhr, textStatus, thrownError) {
                            alert('Something went to wrong.Please Try again later...');
                        }
                    });
            event.preventDefault();
        } );
</script>

Ajax Call

&#13;
&#13;
public function store(Request $request)
{
        $content = $request->input( 'comment-msg' );
        $userID = $request->input( 'userID' );
        $eventID = $request->input( 'eventID' );

    $response=Comment::create([

        'user_id'  =>  $userID,
        'event_id'  =>  $eventID,
        'message'  =>  $content,
    ]);
    return response()->json($response);
}
&#13;
&#13;
&#13;

控制器

Route::resource('comments', 'CommentsController'); 

路线

{{1}}

0 个答案:

没有答案