Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对laravel中控制器的Ajax调用:错误代码422_Laravel - Fatal编程技术网

对laravel中控制器的Ajax调用:错误代码422

对laravel中控制器的Ajax调用:错误代码422,laravel,Laravel,我正在尝试对laravel中的一个控制器进行Ajax调用,得到一个错误代码422 这是我的控制器呼叫: Route::Post('/contact', 'PatientsController@store'); 插入store函数,我做了一些验证 $(document).ready(function () { $('#form-data').submit(function (event) { $.ajaxSetup({ headers: {

我正在尝试对laravel中的一个控制器进行Ajax调用,得到一个错误代码422

这是我的控制器呼叫:

Route::Post('/contact', 'PatientsController@store'); 
插入store函数,我做了一些验证

$(document).ready(function () { 

    $('#form-data').submit(function (event) {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

         var _token= $('input[name=_token]').val();
        var fullname = $('#fullname').val();
        var doctor = $('#doctor').val();
        var date = $('#date').val();
         var reason = $('#reason').val();
         $.ajax({
            type: "POST",
            url: '/contact', // This is what I have updated
            datatype : "application/json",
            contentType: "text/plain",
            data: {                 
                '_token': _token, 
                 'fullname': fullname,
                'doctor': doctor,
                  'date': date,
                'reason': reason,
            }
        })
        .done(function( msg ) {
            console.log(msg);
        });
        event.preventDefault();
    });

});

确保你的声明是正确的

{{ csrf_field() }} or @csrf 
内表。
在这次打击之后,如下所示:

$('#form-data').submit(function (e) {
   e.preventDefault(); // prevent from unwanted page reload.
    $.ajax({
       method: "post",
       url: '{{ route('routeNamehere') }}', // you can call {{ url("/urlHere")}}
       dataType : "json",
       contentType: "text/plain",
       data: $(this).serialize(),
       success:function(response){
          // some code for response
       },
       error:function(error){
          // some code for error here
       }      
     })
 });

您的“完成”和“失败”过程也可以。值得一试,看看这个答案。我认为您的数据库中有一些必填字段,这就是显示错误的原因,您可以向我们显示您的控制台吗?422是无效数据的自然响应代码。检查通过数据传递的所有值。采用如下
“{{csrf\u field()}}”
\u标记值。ajaxSetup在Laravel中不是必需的,它已经在那里完成了。感谢您的回复。。。但是我收到了这个消息{message:“给定的数据无效。”,…}错误:{fullname:[“fullname字段是必需的。”],doctor:[“doctor字段是必需的。”],…}消息:“给定的数据无效。”@Gaiya你能分享你的表单html代码plz吗?也是控制器