[英]Laravel 5.5 : 419 unknown status with AJAX
I am requesting POST
:我请求
POST
:
Route :路线 :
Route::post('/register','CommonController@postRegister')->name('register');
CSRF Meta tag : CSRF 元标记:
<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 :和例外:
The exception comes sometimes and sometimes the code runs smoothly, I have no idea what am i missing here.有时会出现异常,有时代码运行顺利,我不知道我在这里错过了什么。
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 正文中包含令牌,而不是像上面那样。
In addition to putting the crsf-token value in the header meta tag you need to pass it through in your AJAX requests like:除了将 crsf-token 值放在头元标记中之外,您还需要在 AJAX 请求中传递它,例如:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
No need to setup ajax separately.无需单独设置ajax。 Laravel automatically generates a CSRF "token" for each active user session managed by the application.
Laravel 会为应用程序管理的每个活动用户会话自动生成一个 CSRF “令牌”。 Get the token with this:
用这个获取令牌:
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',
...
})
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.