php - jQuery automatically updated value on view when button clicked -


i create button current value on database , when button has been clicked value must automatically updated on view. currently, value updated on database, not automatically updated on view.

here jquery code:

<script type="text/javascript">     $(document).ready(function(){         $('#like').on('click', function(e){             var id = '{{$news->id}}';             $.get('{{ url('view/like')}}/'+id, function(data){                 console.log(id);                 console.log(data);                 $('#like_data').empty();                 $.each(data, function(index, element){                     $('#like_data').append("<p>"+this.like+"</p>");                 });             });         });     }); </script> 

here controller:

public function like($id){      $news = news::find($id);      $news->like += 1;     $news->save();      return $news; } 

you need pass json response controller. this

public function like($id){      $news = news::find($id);      $news->like += 1;     $news->save();      return response()->json([$news], 200); } 

you don't need use $.each() response because send single object response. write code:

$('#like_data').html("<p>"+data.like+"</p>"); 

if want use each write:

 $.each(data, function (index, element) {       $("#like_data").append("<p>" + element.like + "</p>");  });  

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -