Javascript Dropzone - 未捕获的错误:未提供 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41997927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:48:57  来源:igfitidea点击:

Dropzone - Uncaught Error: No URL provided

javascriptjqueryhtmlajaxdropzone.js

提问by Milano

I can't figure out how to get JSONresponse after uploading a file using Dropzonejs.

使用 Dropzonejs 上传文件后,我不知道如何获取 JSONresponse。

I have just this:

我只有这个:

<script src="{% static "dropzone/dropzone.js" %}"></script>

<form id="id_dropzone" class="dropzone" action="/ajax_file_upload_handler/"
              enctype="multipart/form-data" method="post"></form>

I think it is not possible without manually initializing dropzone so I changed it to:

我认为不手动初始化 dropzone 是不可能的,所以我将其更改为:

$("#id_dropzone").dropzone({
                maxFiles: 2000,
                url: "/ajax_file_upload_handler/",
                success: function (file, response) {
                    console.log(response);
                }
            });


<form id="id_dropzone" class="" action=""
              enctype="multipart/form-data" method="post"></form>

Which return Uncaught Error: No URL provided.

哪个返回 Uncaught Error: No URL provided.

How can I initialize dropzone so I can add an options like maxFiles, maxSize and get JSONresponse?

如何初始化 dropzone 以便添加诸如 maxFiles、maxSize 之类的选项并获得JSON响应?

回答by lin

No URL provided happens when a Dropzone gets attached to an object without either:

当 Dropzone 附加到一个对象时,没有提供 URL 发生:

  • an action attribute on a form that tells dropzone where to post
  • a configuration for specific dropzone
  • 表单上的 action 属性,告诉 dropzone 在哪里发布
  • 特定 dropzone 的配置

My bet is, that you have a race condition, where Dropzone attaches itself to an element before you configured it. Make sure that your configuration is either directly after the JS import, or that you set Dropzone.autoDiscover = false;and instantiate the Dropzone explicitly.

我敢打赌,你有一个竞争条件,在你配置它之前,Dropzone 将自己附加到一个元素上。确保您的配置直接在 JS 导入之后,或者您Dropzone.autoDiscover = false;明确设置和实例化 Dropzone。

Take a look over herefor more information.

看看这里了解更多信息。

<script src="{% static "dropzone/dropzone.js" %}"></script>

<script type="text/javascript">

   Dropzone.autoDiscover = false;

   $(document).ready(function () {
        $("#id_dropzone").dropzone({
            maxFiles: 2000,
            url: "/ajax_file_upload_handler/",
            success: function (file, response) {
                console.log(response);
            }
        });
   })

</script>

<form id="id_dropzone" 
      class="dropzone" 
      action="/ajax_file_upload_handler/"
      enctype="multipart/form-data" 
      method="post">
</form>