评论系统使用ajax与laravel,无需重新加载页面


comment system using ajax with laravel without reloaded page

我想在我的网站上添加一个评论系统,使用AJAX和Laravel来加载新的评论,而无需刷新整个页面。

我总是有这样的错误:

Ajax Post (Internal Server Error)

下面是我的代码:
view page :
{!! Form::open(array('action' => array('PersonsController@newComment', $person->id))) !!}          
<div class="form-group">
{!! Form::label('name', 'Nom ') !!}
{!! Form::text('name', null, ['class' =>'form-control', 'style'=>'border-radius: 0']) !!}
</div>  
<div class="form-group">
{!! Form::label('mail', 'E-mail ') !!}
{!! Form::text('mail', null, ['class' =>'form-control', 'style'=>'border-radius: 0']) !!}
</div>  
<div class="form-group">
{!! Form::label('comment', 'Contenu ') !!}
{!! Form::textarea('comment', null, ['class' =>'form-control', 'style'=>'border-radius: 0']) !!}
</div>  
<div class="form-group">
{!! Form::submit('Publier', array('class'=>'send-btn')) !!}               
</div>  
{!! Form::close() !!}
route page :
Route::get('{id}/apropos','PersonsController@show');
Route::post('{id}/apropos','PersonsController@newComment');

控制器页面:

public function newComment($id) {
    if ($request::ajax()) {
        $name = Input::get('name');
        $mail = Input::get('mail');
        $comment = Input::get('comment');
        $Comments = new 'App'Comment;
        $Comments->Persons_id = $id;
        $Comments->date = 'Carbon'Carbon::now();
        $Comments->name=$name;
        $Comments->mail=$mail;
        $Comments->comment=$comment;
        $Comments->save();
        $reponse =array(
            'status' => 'success',
            'msg' => 'Setting created successfully',
        );
        return 'Response::json($response);
    } else {
        return 'no';
    }
}
Ajax页面:
$.ajaxSetup({  
   headers: {  
           'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')  
    } 
});      
$(document).ready(function() {
    $('.send-btn').click(function (e) {
        e.preventDefault();
        var name = $('#name').val();
        var mail = $('#mail').val();
        var comment = $('#comment').val();
        $.ajax({
            type: "POST",
            url: '{{url("1/apropos")}}',
            dataType: 'JSON',
            data: {name: name, mail: mail, comment: comment},
            success: function( data ) {
            $("body").append("<div>"+data+"</div>");                
            }
        });
    });
 });          

我认为你的ajax页面是在同一页的视图(因为你使用。send-btn)。

使用$.post()更简单,因为你不需要配置那么多参数。https://api.jquery.com/jquery.post/

你有关于Ajax Post 500 (Internal Server error)错误的更多信息吗?你没有留言吗?在$.ajax上使用错误函数:

如果请求失败,则调用

错误回调选项。它接收jqXHR、一个指示错误类型的字符串和一个异常对象如果适用。一些内置错误将提供字符串作为异常对象:"abort", "timeout", "No Transport".

所以你可以在AJAX请求中添加如下内容:

error: function (xhr, textStatus, thrownError) {
    console.log(xhr.status);
    console.log(xhr.statusText);
    console.log(textStatus);
    console.log(thrownError);
  }