【发布时间】:2017-12-20 08:36:16
【问题描述】:
我正在使用 dropzone http://www.dropzonejs.com/ 但作为 vuejs 组件:https://github.com/rowanwins/vue-dropzone
我也在使用 laravel,它有一个叫做 csrf-token 的东西,它应该与每个请求一起发送到服务器,以便服务器可以验证请求是否合法。
我需要在使用 dropzone 上传图片时发送此令牌。 Laravel 文档建议这样做:
https://laravel.com/docs/5.4/csrf#csrf-x-csrf-token
Then, once you have created the meta tag, you can instruct a library like
jQuery to automatically add the token to all request headers. This provides
simple, convenient CSRF protection for your AJAX based applications:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
By default, the resources/assets/js/bootstrap.js file registers the value of
the csrf-token meta tag with the Axios HTTP library. If you are not using
this library, you will need to manually configure this behavior for your application.
所以在我安装的 dropzone 组件中,我放置了以下代码:
mounted () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
当我的 dropzone 将文件上传到服务器时,由于缺少 csrf-token,我从服务器收到异常。我还检查了发送到服务器的标头,但令牌不存在。
我的问题,dropzone 是否真的使用 ajax 来发送图片?如果是,那么为什么上面的代码不添加正确的标头,如果它不使用 ajax 发送标头,那么我该如何设置正确的标头?
【问题讨论】:
-
您将
csrf-token存储在哪里? -
在我的布局刀片文件中
-
它在 HTML 元标记中吗?
-
是的:
-
我在 laravel 上工作不多,所以你能在数据选项中使用
csrf_token()设置 csrf-token 吗?
标签: ajax laravel vue.js dropzone.js