laravel 5.6的ajax帖子

时间:2018-05-22 23:24:24

标签: ajax laravel

我无法弄清楚如何发布此ajax请求。

     <button class="btn btn-sm btn-primary" id="ajaxSubmit">Submit</button>

      <textarea rows="4" class="form-control resize_vertical" id="application_notes" name="application_notes" placeholder="Notes">{{$application->notes}}</textarea>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  var url = "/instructor-notes-save/{{$application->id}}"
      $(document).ready(function(){
         $('#ajaxSubmit').click(function(e){
               e.preventDefault();
               $.ajaxSetup({
                  headers: {
                      'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                  }
              });
            $.ajax({

                  url: url,
                  method: 'post',
                  data: {
                     application_notes: jQuery('#application_notes').val(),
                  },
                  success: function(response){
                     console.log(response);
                  }});
               });
            });
</script>

我的控制器是:

public function saveNotes(Request $request, $id)
    {
      $application = Application::findOrFail($id);
      $application->notes = $request->application_notes;
      $application->save();
      return response()->json(['success'=>'Data is successfully added']);
    }

对于它的价值,这是我的路线:

Route::post('/instructor-notes-save/{id}', 'InstructorsController@saveNotes')->name('instructor.save.note');

为了让这个ajax请求起作用,我缺少什么?在我的控制台日志中,我收到419未知状态错误。

1 个答案:

答案 0 :(得分:0)

在您的html <head>中添加以下标记:

<meta name="csrf-token" content="{{ csrf_token() }}">