dongyong9224 2016-08-10 15:35
浏览 46

评论系统使用带有laravel的ajax而不重新加载页面

I want to add a comment system on my website using AJAX with Laravel to load new comments without refreshing whole page.

I always have this error :

Ajax Post 500 (Internal Server Error)

below is my code:

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');

Controller page :

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 page :

$.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>");                
            }
        });
    });
 });          
  • 写回答

1条回答 默认 最新

  • duangu9173 2016-08-10 15:58
    关注

    I suppose that your ajax page is in the same page than the view (because you are using .send-btn).

    It´s easier to use $.post() because you don´t need to configure so many parameters. https://api.jquery.com/jquery.post/

    Do you have more information about the error: Ajax Post 500 (Internal Server Error). Don´t you have any message? Use error function on $.ajax:

    error callback option is invoked, if the request fails. It receives the jqXHR, a string indicating the error type, and an exception object if applicable. Some built-in errors will provide a string as the exception object: "abort", "timeout", "No Transport".

    so you can add at your AJAX request something like:

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

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错