gpt4 book ai didi

javascript - Laravel TokenMismatchException 和 dropzone

转载 作者:行者123 更新时间:2023-11-27 23:26:52 25 4
gpt4 key购买 nike

我正在尝试通过 dropzone 上传图片,但即使我在需要的地方添加了 csrf token ,我也收到 token 不匹配错误,我变得非常绝望......

我的表单

{!! Form::open(['route' => 'photo.upload', 'id' => 'hello', 'method' => 'POST', 'class' => 'dropzone no-margin dz-clickable']) !!}
<div class="dz-default dz-message"><span>Drop files here to upload</span></div></form>
{!! Form::close() !!}

我的脚本

Dropzone.autoDiscover = false;

Dropzone.options.hello = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 5, // MB
parallelUploads: 2, //limits number of files processed to reduce stress on server
addRemoveLinks: true,
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
},
accept: function(file, done) {
// TODO: Image upload validation
done();
},
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", $('input[name="_token"]').val() ); // Laravel expect the token post value to be named _token by default
},
init: function() {
this.on("success", function(file, response) {
// On successful upload do whatever :-)
});
}
};

// Manually init dropzone on our element.
var myDropzone = new Dropzone("#hello", {
url: $('#hello').attr('action')
});

请求 header

...
X-CSRF-Token:P4wc9NVVZJe1VjalPwO6d6WQXZ9eEqPd84ICpToG
...

请求负载

------WebKitFormBoundarySKMUFNO6dbgzeQVK
Content-Disposition: form-data; name="_token"

P4wc9NVVZJe1VjalPwO6d6WQXZ9eEqPd84ICpToG
------WebKitFormBoundarySKMUFNO6dbgzeQVK
Content-Disposition: form-data; name="_token"

P4wc9NVVZJe1VjalPwO6d6WQXZ9eEqPd84ICpToG
------WebKitFormBoundarySKMUFNO6dbgzeQVK
Content-Disposition: form-data; name="file"; filename="Screen Shot 2016-01-14 at 18.27.40.png"
Content-Type: image/png


------WebKitFormBoundarySKMUFNO6dbgzeQVK--

当我查看生成的表单时,有 csrf 字段

<input name="_token" type="hidden" value="P4wc9NVVZJe1VjalPwO6d6WQXZ9eEqPd84ICpToG">

你知道为什么即使我把 crsf token 放在应该的地方它也不起作用吗?

感谢您的宝贵时间。

最佳答案

只需将隐藏字段放置在表单中,就像这样

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

您可以通过使用 ajax 调用传递 token 值来实现不同的方式,例如

$(function () {
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
});

关于javascript - Laravel TokenMismatchException 和 dropzone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34899317/

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