gpt4 book ai didi

javascript - Laravel - Dropzone.js 的 TokenmissmatchException

转载 作者:行者123 更新时间:2023-11-28 01:01:03 25 4
gpt4 key购买 nike

我使用 dropzone.js 将图像上传到我的网站,但 Laravel 总是报告 TokenMismatchException,尽管我在表单中使用 Form::open() ,它会自动添加 _token .

这是我的代码:

{{ Form::open(["class" => "dropzone", "id" => "imgUpload", "action" => "UploadsController@uploadImage"]) }}
<div class="fallback">
{{ Form::submit() }}
</div>
{{ Form::close() }}

js

Dropzone.options.imgUpload = {
paramName: "file",
dictDefaultMessage: "Pošalji sliku",
acceptedFiles: "image/*",
previewsContainer: ".dropzone-previews",
uploadprogress: function(progress, bytesSent){
console.log(progress);
}
};

如何解决这个问题?

最佳答案

通过 AJAX 发布时,Dropzone 似乎不包含 token 。你可以使用类似的东西来让它这样做......

Dropzone.options.imgUpload = {
paramName: "file",
dictDefaultMessage: "Pošalji sliku",
acceptedFiles: "image/*",
previewsContainer: ".dropzone-previews",
uploadprogress: function(progress, bytesSent) {
console.log(progress);
},
sending: function(file, xhr, formData) {
// Pass token. You can use the same method to pass any other values as well such as a id to associate the image with for example.
formData.append("_token", $('[name=_token']).val()); // Laravel expect the token post value to be named _token by default
}
};

我在这里找到了该片段... http://laravel.io/forum/04-17-2014-tokenmismatchexception-with-dropzonejs

通过 AJAX 提交的 token 和表单似乎也存在一些问题。在这种情况下,您将需要在初始化 dropzone 时包含附加 header 。

Dropzone.options.imgUpload = {
paramName: "file",
dictDefaultMessage: "Pošalji sliku",
acceptedFiles: "image/*",
previewsContainer: ".dropzone-previews",
headers: {
"X-CSRF-Token": $('[name=_token').val())
},
uploadprogress: function(progress, bytesSent) {
console.log(progress);
}
};

要利用此功能,请修改 filters.php 中的 CSRF 过滤器,以检查该 header (如果我们通过 AJAX 提交)。

Route::filter('csrf', function() 
{
$token = Request::ajax() ? Request::header('X-CSRF-Token') : Input::get('_token');

if (Session::token() != $token) {
throw new Illuminate\Session\TokenMismatchException;
}
});

关于javascript - Laravel - Dropzone.js 的 TokenmissmatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25627269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com