Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

laravel - I make Comment by Ajax but its not working

I make Comment by Ajax but its not working. It can insert and I already recived the data form controller. I check alert is display but html not display. Here is my Route: Route::post('/addComment', 'LessonController@addComment'); Here is my controller

public function addComment(Request $request)
{
  $lesson_id = $request->lesson_id;
  $name = $request->name;
  $body= $request->body;

  $comment = new Comment();
  $comment->name = $name ;
  $comment->body = $body;
  $comment->lesson_id = $lesson_id;
  $comment->save(); 
 return response()->json($comment);

}

Here is view comment create

<div class="leave_review">
                <h3 class="blog_heading_border"> Leave a Comment </h3>
                {!! Form::open(['action' => 'LessonController@addComment', 'method' => 'POST', 'id' => 'comment' ]) !!}

                  <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                  <input type="hidden" id="lesson_id" name="lesson_id" value="{{$lesson->id}}" /> 

                </div>
                <div class="row">
                  <div class="col-sm-6">
                    {{Form::label('name','Name')}}
                    {{Form::text('name', '', ['class' => 'form-group', 'id'=>'name'])  }}
                  </div>
                <div class="col-sm-12">
                  {{Form::label('body','Message')}}
                  {{Form::textarea('body', '', ['class' => 'form-group', 'id'=>'body'])  }}
                    </div>
                    </div>
                    <div class="row">
                    <div class="col-md-12">
                    </div>
                </div>
                {{Form::submit('Submit', ['name'=>'submit', 'class' => 'mt_btn_yellow pull-right'])}}
                {!! Form::close() !!}
                <br>

Here is comment display view blade

 <div id="commen_show">
                  @foreach ($comment as $item) 
                      <ol class="review-lists">
                      <li class="comment">
                      <div class="activity_rounded">
                      <img src="/storage/Iconimage/about.png" alt="image"> </div>
                      <div class="comment-body">
                      <h4 class="text-left">{{$item->name}} &nbsp;&nbsp;
                          <small class="date-posted pull-right">{{$item->created_at}}</small>
                      </h4>
                      <p>{{$item->body}}</p>               
                      <a href="#" class="pull-left">Reply</a>
                      <div class="clearfix"></div>
                      </div>
                      </li>
                      </ol>
                  @endforeach
                </div>
                  </div>  

Here is my javascript, I think something wrong with append

<script>
      $(document).ready(function($){
        $('#comment').on('submit', function(){
          var lesson_id  = $('#lesson_id').val();
          var name       = $('#name').val();
          var body       = $('#body').val();
          var _token     = $('input[name="_token"]').val();

          $.ajax({
                          type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
                          url         : "{{url('/addComment')}}",  // the url where we want to POST
                          data: {_token:_token, lesson_id:lesson_id, body:body, name:name},
                          success: function(response) {    
                            if(response){

                     $('#commnet_show').prepend(
                                `  <ol class="review-lists">
                    <li class="comment">
                    <div class="activity_rounded">
                    <img src="/storage/Iconimage/about.png" alt="image"> </div>
                    <div class="comment-body">
                    <h4 class="text-left">${response.name} &nbsp;&nbsp;
                        <small class="date-posted pull-right">${response.created_at}</small>
                    </h4>
                    <p>${response.body}</p>               
                    <a href="#" class="pull-left">Reply</a>
                    <div class="clearfix"></div>
                    </div>
                    </li>
                    </ol>  `
                              ;);
                              }
                             }                  
                      });
                   });
                });

  </script>  
question from:https://stackoverflow.com/questions/66058795/i-make-comment-by-ajax-but-its-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...