Jquery AJAX post未向控制器发送数据

Jquery AJAX post未向控制器发送数据,jquery,ajax,laravel,Jquery,Ajax,Laravel,也许这是一个非常常见的话题,但我找不到任何解决方案!,当我尝试console.log时,我尝试将ajax post发送到控制器,数据在那里,但数据没有发送到我的控制器,这是屏幕截图my console.log,我需要将一些数据从jquery ajax发送到laravel控制器,这样我就有一个带有csrf令牌的元,当我使用ajax将post请求发送到URL时,它只发送令牌!!但我没有给它发送任何数据 这是我的javaScript代码:- function doComment(lesson_id,

也许这是一个非常常见的话题,但我找不到任何解决方案!,当我尝试console.log时,我尝试将ajax post发送到控制器,数据在那里,但数据没有发送到我的控制器,这是屏幕截图my console.log,我需要将一些数据从jquery ajax发送到laravel控制器,这样我就有一个带有csrf令牌的元,当我使用ajax将post请求发送到URL时,它只发送令牌!!但我没有给它发送任何数据

这是我的javaScript代码:-

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();
            }
          }
      });
    }
  }
这是我的控制器

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);
    }
这是我的错误:-

我不确定laravel如何处理post数据(是否处理正常的$\u post和php://input). 您可以尝试从Javascript Ajax代码中删除
datatype:JSON
。删除
缓存:false,
contentType:false,

processData:false,
太多;这些参数是可选的,您应该让它使用默认值,除非有特殊原因需要json数据类型。同样,您可以尝试使用
$received=file\u get\u contents(“php://input");然后
打印($received)
。查看它是否捕获到JSON数据

由于代码中有错误而未发送数据,请尝试删除error@sanojlawrence这不是我的javascript代码中的错误,这是因为数据没有发送到我的控制器您有两个错误一个是
'contributor\u id'=>str\u replace('}','',str\u replace('{“contributor\u id:','','$contri))
和另一个日期时间变量$now.show.me的路线代码doComment@AmmarAli我在删除时添加了路由代码,错误显示为ilegal调用未捕获类型错误:非法调用
Route::post('lessons/coments/doComment','Web\LessonsController@doComment');