Laravel 5.5:419 未知状态与 AJAX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48680637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:21:21  来源:igfitidea点击:

Laravel 5.5 : 419 unknown status with AJAX

phpajaxformslaravelcsrf

提问by Gammer

I am requesting POST:

我要求POST

Route :

路线 :

Route::post('/register','CommonController@postRegister')->name('register');

Route::post('/register','CommonController@postRegister')->name('register');

CSRF Meta tag :

CSRF 元标记:

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

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

$("#submitSalonForm").click(function(e) {
  $.ajaxSetup({
      headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
  });
  $.ajax({
      url: "/register",
      type: "post",
      data: new FormData($('form')[1]),
      cache: false,
      contentType: false,
      processData: false,
      success:function(response) {
          return alert('Form 2 submitted');
      }
  });
});

And the exception :

和例外:

Exception screenshot

异常截图

The exception comes sometimes and sometimes the code runs smoothly, I have no idea what am i missing here.

有时会出现异常,有时代码运行顺利,我不知道我在这里错过了什么。

采纳答案by Leo

Change ajax method from post to get

将 ajax 方法从 post 更改为 get

<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">

Ajx call:

Ajx 调用:

let formData = $('form').serializeArray();
$.ajax({
      url: "/register",
      type: "POST",
      data: {formData, "_token": $('#token').val()},
      cache: false,
      datatype: 'JSON',
      processData: false,
      success: function (response) {
           console.log(response);
         },
         error: function (response) {
           console.log(response);
         }
  });

Your route is get

你的路线是

Route::get('/register','CommonController@showRegister')->name('register');

Ajax call is making a post request, laravel sqwaks with a http exception.

Ajax 调用正在发出一个 post 请求,laravel sqwaks 有一个 http 异常。

EDIT:Laravel 419 post error is usually related with api.php and token authorization

编辑:Laravel 419 post 错误通常与 api.php 和令牌授权有关

So try to include the token on ajax body instead like above.

因此,请尝试在 ajax 正文中包含令牌,而不是像上面那样。

回答by smartrahat

No need to setup ajax separately. Laravel automatically generates a CSRF "token" for each active user session managed by the application. Get the token with this:

无需单独设置ajax。Laravel 会为应用程序管理的每个活动用户会话自动生成一个 CSRF “令牌”。用这个获取令牌:

var token = "{{ csrf_token() }}";

Pass the token in data

传入令牌 data

var token = "{{ csrf_token() }}";
var formData = new FormData($('form')[1])
$.ajax({
    url : '/register',
    data : {_token:token,formData:formData},
    type: 'post',
    ...
})

回答by Mohammad ALTAWEEL

Well, I know I am too late, but I did face the exact issue you face.

好吧,我知道我为时已晚,但我确实遇到了您面临的确切问题。

I was 100% sure that the token has been sent with the request, but the issue is still.

我 100% 确定令牌已与请求一起发送,但问题仍然存在。

So, after too many searches I finally got it fixed by following these steps:

因此,经过过多的搜索,我终于按照以下步骤修复了它:

php artisan config:clear          
php artisan view:clear
php artisan route:clear
php artisan cache:clear
php artisan clear-compiled