我尝试使用 AJax、Jquery、Laravel 上传图像。在这里,我尝试了很多解决方案,但仍然收到 419 错误,在某些情况下收到 500 内部错误。
我尝试过的代码是
<form method="POST" id="needs" novalidate enctype="multipart/form-data">
{{csrf_field()}}
<input name="image1" id="image1" type="file" class="form-control" required="" />
<br>
<input type="file" name="image2" id="image2" class="form-control" required/>
<br>
<input type="file" name="image3" id="image3" class="form-control" required/>
<br>
<input type="file" name="image4" id="image4" class="form-control" required/>
<br>
<input type="file" name="image5" id="image5" class="form-control" required />
<br>
<button type="button" id="upload_image" name="upload_image" class="btn btn-lg btn-success" onclick="image_up();">Upload</button>
</form>
Jquery:
function image_up()
{
alert("Uploading start");
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{route('collage.store')}}",
type: 'POST',
data : new FormData($(this)[0]),
dataType: "json",
cache : false,
processData: false,
success: function () {
alert('form was submitted');
}
});
}
路线:
Route::post('/', 'CollagePrimController@post')->name('collage.store');
请任何人帮我解决这个问题。提前致谢
最佳答案
Laravel 419 状态错误仅与 token 授权相关。
在您的 head 部分添加以下代码:
<meta name="csrf-token" content="{{ csrf_token() }}">
将以下代码添加到您的 ajax 调用中:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
如果仍然出现 419 错误,则通过修改 app/Http/Middleware/VerifyCsrfToken.php 禁用特定路由中的 CSRF token
class VerifyCsrfToken extends BaseVerifier
{
// The URIs that should be excluded from CSRF verification.
protected $except = [
"/*"
];
}
关于php - Laravel 中的 Ajax 419 状态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47199645/