How to include the CSRF token in the headers in Dropzone upload request?

前端 未结 9 1825
难免孤独
难免孤独 2020-12-13 18:19

I am working on a single page application and I am using Laravel 5 for the web service.

All forms are submitted asynchronously and I use a beforeSend on them to atta

9条回答
  •  天命终不由人
    2020-12-13 18:48

    Dropzone.autoDiscover = false;
            // or disable for specific dropzone:
            // Dropzone.options.myDropzone = false;
    
            $(function () {
                // Now that the DOM is fully loaded, create the dropzone, and setup the
                // event listeners
    
                var myDropzone = new Dropzone("#my-awesome-dropzone");
                myDropzone.on("addedfile", function (file) {
                    /* Maybe display some more file information on your page */
                });
                myDropzone.on("sending", function (file, xhr, formData) {
                     formData.append('csrfmiddlewaretoken', document.getElementsByName('csrfmiddlewaretoken')[0].value);
                    /* Maybe display some more file information on your page */
                });
            });
    

    You could include it this way.

提交回复
热议问题