我正在評論部分(對帖子發表評論),我可以在沒有刷新頁面的情況下存儲用戶點擊的評論,但無法立即在該帖子上顯示這些評論,而無需刷新頁面。erorr:通過ajax顯示評論(來自數據庫)(laravel 5.2)
我的觀點:
<ul id="templates" class="dropdown-menu" role="menu">
<div class="row">
<section class="col-md-offset-1 col-md-3 col-xs-3 "><a class="btn btn-sm" id="Nice" data-postid="{{$post->id}}">Nice!</a>
</section></ul>
@if($storage->comment==0)
<div class="row" id="zero">
{{$storage->textcomment}}
</div>
@endif
我的腳本:
<script>
$(document).ready(function(){
$('.dropdown-menu a').click(function(){
var action=$(this).attr('id');
var id=$(this).attr('data-postid');
$.ajax({
method:'POST',
url:{{route('comment123')}},
data:{id:id,action:action}
})
.done(function(msg) {
$('#zero').text(msg['new_bod']);
});
});
});
</script>
我的控制器:
public function comment123(Request $request){
$qw = storage::find($request['id']);
$qw->textcomment = $request['action'];
$qw->save();
return response()->json(['new_bod' => $qw->textcomment],200);
}
我的路線:
Route::post('/comment123' , [
'uses' => '[email protected]',
'as' => 'comment123'
]);
如果要顯示最近添加的評論,而不是爲什麼要顯示來自數據庫 – DsRaj
@DsRaj的數據,以便用戶再次登錄時,評論將出現在那裏。 –