Maybe this is a very common topic, but i can't find any solution!, I try to make ajax post to a controller when i try to console.log, the data is there but data is not sent to my controller, this is screenshot my console.log, I need to send some data from jquery ajax to a laravel controller, so that i have a meta with the csrf token, when i send the post request to an URL using ajax, it just sends the token!! but nothing of the data i give it to send.
This is my javaScript code:-
function doComment(lesson_id, parent_id) {
var body = $('#textbody'+parent_id).val();
var file_data = $('#image').prop("files")[0];
if (body == '') {
alert('Harap Isi Komentar !')
}else {
var postData =
{
"_token":$('meta[name="csrf-token"]').attr('content'),
"lesson_id": lesson_id,
"parent_id": parent_id,
"image" : image,
"body": body
}
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type :'POST',
url :'{{ url("lessons/coments/doComment") }}',
dataType: 'json',
cache: false,
contentType: false,
processData: false,
data : postData,
beforeSend: function(){
console.log(postData);
// Show image container
swal({
title: "Sedang mengirim Komentar",
text: "Mohon Tunggu sebentar",
imageUrl: "{{ asset('template/web/img/loading.gif') }}",
showConfirmButton: false,
allowOutsideClick: false
});
{{-- $("#loader").show(); --}}
},
success:function(data){
if (data.success == false) {
window.location.href = '{{ url("member/signin") }}';
}else if (data.success == true) {
$('#textbody'+parent_id).val('');
swal({
title: "Komentar anda sudah terkirim!",
showConfirmButton: true,
timer: 3000
});
getComments();
}
}
});
}
}
and this is my controller
public function doComment()
{
$response = array();
if (empty(Auth::guard('members')->user()->id)) {
$response['success'] = false;
} else {
$now = new DateTime();
$uid = Auth::guard('members')->user()->id;
$body = Input::get('body');
$lesson_id = Input::get('lesson_id');
$member = DB::table('members')->where('id', $uid)->first();
$lessons = DB::table('lessons')->where('id', $lesson_id)->first();
$parent_id = Input::get('parent_id');
$contri = Lesson::where('id',$lesson_id)
->select('contributor_id')
->first();
// dd($lesson_id);
$image = Input::file('image');
// dd($image);
$lessonsDestinationPath= 'assets/source/komentar';
if(!empty($image)){
$imagename = $image->getClientOriginalExtension();
$image->move($lessonsDestinationPath, $imagename);
}else{
$imagename = '';
}
if($imagename ==''){
$url_image= $imagename;
}else{
$urls=url('');
$url_image= $urls.'/assets/source/komentar/'.$imagename;
}
$store = DB::table('comments')->insertGetId([
'lesson_id' => $lesson_id,
'member_id' => $uid,
'body' => $body,
'parent_id' => $parent_id,
'status' => 0,
'desc' => 0,
'images' => $url_image,
'contributor_id' => str_replace('}','',str_replace('{"contributor_id":', '',$contri)),
'created_at' => $now,
'updated_at' => $now,
]);
$getmembercomment = DB::table('comments')
->Join('members','members.id','=','comments.member_id')
->where('comments.lesson_id',$lesson_id)
->where('comments.parent_id',0)
->where('comments.status',1)
->select('comments.*','members.username as username')
->first();
$getemailchild = DB::table('comments')
->Join('comments as B', 'comments.id', 'B.parent_id')
->Where('B.parent_id', $parent_id)
->where('comments.member_id', '<>', 'B.member_id')
->select('comments.member_id as tanya', 'B.member_id as jawab')->distinct()
->get();
// dd($getemailchild);
if($parent_id != 0){
foreach ($getemailchild as $mails) {
// Check type
if (is_array($mails)){
// Scan through inner loop
foreach ($mails as $value) {
$member = Member::Find($value);
$lesson = Lesson::Find($lesson_id);
$contrib = Contributor::find($lessons->contributor_id);
$member->notify(new UserReplyNotification($member, $lesson, $contrib));
}
}
}
}
if ($store) {
// dd($store);
DB::table('contributor_notif')->insert([
'contributor_id' => $lessons->contributor_id,
'category' => 'Komentar',
'title' => 'Anda mendapat pertanyaan dari ' . $member->username,
'notif' => 'Anda mendapatkan pertanyaan dari ' . $member->username . ' pada ' . $lessons->title,
'status' => 0,
'created_at' => $now,
]);
$member = Member::Find($uid);
$comment = Comment::Find($store);
$lesson = Lesson::find($lessons->id);
$contrib = Contributor::find($lessons->contributor_id);
$contrib->notify(new UserCommentNotification($member, $comment, $contrib, $lesson));
$response['success'] = true;
}
}
echo json_encode($response);
}
this is my route :
Route::post('lessons/coments/doComment','Web\LessonsController@doComment');
'contributor_id' => str_replace('}','',str_replace('{"contributor_id":', '',$contri))
and another dateTime variable $now. – rkj