带有laravel 5.6的ajax发布 [英] ajax post with laravel 5.6

查看:55
本文介绍了带有laravel 5.6的ajax发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何获取此ajax请求进行发布.

I can't figure out how to get this ajax request to post.

     <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']);
    }

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

And for what it's worth, here is my route:

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

要使此ajax请求正常工作,我缺少什么?在我的控制台日志中,我收到419个未知状态错误.

What am i missing to get this ajax request to work? In my console log, i get a 419 unknown status error.

推荐答案

请检查您的布局文件中< head> 中是否存在元标记 _token 标签.

Kindly check that the meta tag _token is present in your layout file inside the <head> tag.

还请确保您的路由文件中包含AJAX网址.

Also please make sure that the AJAX url is present in your routes file.

这篇关于带有laravel 5.6的ajax发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆