2017-07-15 54 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發送到服務器,它會工作。