2017-07-15 51 views
0

我使用的悬浮窗http://www.dropzonejs.com/但作为vuejs成分:https://github.com/rowanwins/vue-dropzone发送正确的头与悬浮窗

我也使用laravel其中有一种叫CSRF令牌这应该与每个请求到服务器,使发送服务器可以验证请求是否合法。

我需要在使用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. 

所以,我的悬浮窗组件内部安装我把这个代码:

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

当我的悬浮窗上载文件,我得到一个异常从服务器服务器,因为csrf-token缺失。我还检查了发送到服务器的标题,但令牌不在那里。

我的问题是,dropzone实际上使用ajax发送图片吗?如果是,那么为什么上面的代码没有添加正确的头文件?如果它没有使用ajax发送头文件,那么我怎样才能设置正确的头文件?

+0

你在哪里存储'csrf-token'? –

+0

我的布局刀片文件 –

+0

它在HTML元标记? –

回答

1

Dropzone vue组件具有标题作为道具。你需要设置这个道具你想要dropzone发送到服务器,它会工作。