I'm trying to create comment section and getting this error while using Ajax in Laravel 5 framework. Though I've tried different solutions, I couldn't figure the problem out. So, any opinion would be greatly appreciated.
The input fields are as follows:
<div class="panel-body">
{!!Form::textarea('comment', null, ['class'=>'form-control_comment', 'id'=>'comment', 'placeholder'=>'Type your comment here...'])!!}
<input type="hidden" name="uid" id="uid" value="{{$user->id}}">
<input type="hidden" name="pid" id="pid" value="{{$post->id}}">
<input name="_token" type="hidden" id="csrf-token" value="{{ csrf_token() }}"/>
<button type="button" class="btn btn-sm btn-success" id= "comment_button" style="float: right; margin: 5px;">Submit</button>
</div>
The Ajax part:
$(document).ready(function(){
$("#comment_button").click(function(){
var token = $("#csrf-token").val();
var a= $("#comment").val();
var uid= $("#uid").val();
var url = "<?php echo Request::root();?>";
$.ajax({
url: url+"post/comment_action",
type: "POST",
data: {"newComment":a, "uid":uid, "pid":pid, "token": token},
success: function (newResult){
newResult= JSON.parse(newResult);
$("#all_responses").append(newResult);
}
});
});
});
And the action function within the controller which is being invoked by the Ajax call:
$new_comment= Input::get('newComment');
$uid= Input::get('uid');
$pid= Input::get('pid');
$com= new Comment;
$com->content= $new_comment;
$com->userId= $uid;
$com->postId= $pid;
$com->save();
$data= array();
$all_comments= Comment::where('postId', $pid)->first();
$u= User::where('id', $uid);
foreach($all_comments as $coms)
{
$data= "<div class=\"response_top_div\"><a href=\"#\">".$u->name."</a> • <small>" . $coms->created_at->diffForHumans() . "</small></div><div class=\"response_div\">" . $coms->content . "</div>";
}
$data= json_encode($data);
echo $data;
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Q45535
via IFTTT
Aucun commentaire:
Enregistrer un commentaire