【问题标题】:Dropzone : Uncaught Error: No URL providedDropzone:未捕获的错误:未提供 URL
【发布时间】:2017-01-04 00:16:52
【问题描述】:

我有以下表格:

<form class="block-center" id="pdfForm" method="POST" action="form_threatment.php" enctype="multipart/form-data" style="margin-top: 30px;">    
  <div class="form-group push-50-t col-md-12">
    <div class="col-md-12">
      <div class="form-material form-material-primary">    
        <div class="dropzone dropzone-previews" id="pdfFile">
        </div>    
      </div>
    </div>
  </div>    
</div>

<div class="form-group push-50-t col-md-6">
  <div class="col-md-12">
    <div class="form-material form-material-primary">                               
      <input class="form-control" name="first_name" type="text" id="first_name" />
      <label for="first_name"><span class="asterix">*</span> Prénom : </label>                      
    </div>
  </div>
</div>

我包含这样的 dropzone.js 库:

<script src="assets/js/dropzone.js"></script>

还有我自己的 dropzone myDropzone.js :

<script src="assets/js/myDropzone.js"></script>

myDropzone.js 文件中,我以这种方式配置了 div#pdfFile:

Dropzone.autoDiscover = false;

$(document).ready(function() {      

    Dropzone.options.pdfFile = {
        // url does not has to be written if we have wrote action in the form tag but i have mentioned here just for convenience sake 
        url: 'form_threatment.php',
        acceptedFiles: '.pdf',
        maxFilesize: 20,
        addRemoveLinks: true,
        autoProcessQueue: false, // this is important as you dont want form to be submitted unless you have clicked the submit button
        uploadMultiple: false,
        autoDiscover: false,
        paramName: 'pdf', // this is optional Like this one will get accessed in php by writing $_FILE['pic'] // if you dont specify it then by default it taked 'file' as paramName eg: $_FILE['file'] 
        previewsContainer: '#pdfFile', // we specify on which div id we must show the files
        clickable: false, // this tells that the dropzone will not be clickable . we have to do it because v dont want the whole form to be clickable 

        accept: function(file, done) {
            console.log("uploaded");
            done();
        },
        error: function(file, msg){
            alert(msg);
        },
        init: function() {
            var myDropzone = this;

            // #submit-all it the id of the submit button
            $("#submit-all").on("click", function(e) {
                var files = $('#pdfFile').get(0).dropzone.getAcceptedFiles();
                console.log(myDropzone);
                console.log(files);
                //e.preventDefault();
                //e.stopPropagation();
                myDropzone.processQueue(); // this will submit your form to the specified action path
                // after this, your whole form will get submitted with all the inputs + your files and the php code will remain as usual 
                //REMEMBER you DON'T have to call ajax or anything by yourself, dropzone will take care of that

            });                                             
        }
    };
    Dropzone.options.pdfFile.init();
});

加载页面时出现错误:

未捕获的错误:未提供 URL。

之前,我修改了 dropzone.js 文件来设置 Dropzone 选项,但我重置了 dropzone.js 库文件并决定在 myDropzone.js 中设置选项强>文件。

dropzone.js 文件中设置选项时,我没有错误,但在我重置这些选项并在 myDropzone.js 中设置它们之后,我有这个错误让我相信 myDropzone.js 中的选项未初始化

事实上,当我点击 #submit-all 按钮时,init() 函数可以正常工作。

请问有什么办法解决这个问题吗?


好的,我解决了:

未捕获的错误:未提供 URL。

删除它。

现在,当我提交时,我收到以下错误:

未捕获的类型错误:myDropzone.processQueue 不是函数

在 init() 函数中。

编辑:

我解决了上一个错误,删除了 processQueue 函数,并阻止了我上传页面的 Validate 按钮,直到 PDF 没有成功上传。 我知道这是一个丑陋的 hack,但我没有找到其他方法。

【问题讨论】:

  • Uncaught Error: No URL provided - 任何指示此错误在哪一行代码上?
  • 它在 dropzone.js 的第 440 行引发,即:if (!this.options.url) { throw new Error("No URL provided."); } 这是在 dropzone.js 库的函数 function Dropzone(element, options) 中。我应该在 dropzone.js 之前导入 myDropzone.js 吗?
  • 绝对不要颠倒这两个文件的顺序 - 你当然没有设置任何 options.url 所以你得到这个错误是有道理的
  • 我在 myDropzone.js 中提供了 url:url: 'form_threatment.php',+ 表单操作也是form_threatment.php
  • 不,那是options.pdfFile.url

标签: javascript jquery dropzone.js


【解决方案1】:

在尝试了所有建议之后,我实际上在treatment_form.php 中通过一个丑陋的解决方法解决了这个问题:

我做了两次威胁:

  1. 收到 PDF 后,我检查一切正常,然后将其上传到我的服务器。
  2. 当用户提交表单时,我会验证表单中的其余信息,并威胁他们知道 PDF 已上传。

【讨论】:

    【解决方案2】:

    不要将你的 dropzone 代码放入 jquery $(document).ready(function(){ });

    【讨论】:

    • 为什么它能解决我的问题?是否禁止在ready() 事件中包装它?
    【解决方案3】:

    我想到了三件事: 您的主要 dropzone 元素(您放置的地方)是#pdfForm 你想要#pdfFiles 中的预览。也就是说:

    1) 代码必须是Dropzone.options.pdfForm 而不是Dropzone.options.pdfFile

    2) Dropzone.options.pdfForm 是让自动发现为真(默认)时的正确语法。但是,如果您希望自动发现为 false,您可以尝试:
    var myDropzone = new Dropzone("#pdfForm", { url: "form_threatment.php", other options... // JS : Dropzone class$("#pdfForm").dropzone({ url: "form_threatment.php", other options... // JS : jquery style

    3) 如果在 JS 中指定“form_threatment.php”,则不必在 HTML 中进行。然后你可以从表单中删除 action="form_threatment.php"

    如果您决定使用 autodiscover=true (defalut),另请参阅我的其他答案,了解 jQuery 版本 3 可能出现的问题:Why is my dropzone javascript form not working?

    【讨论】:

    • 已经做了所有这些..但没有解决问题。我暂时想出了一个丑陋的解决方法,哈哈。
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多