【问题标题】:Dropzone JS Wait until ajax form submitted and change options.urlDropzone JS 等到提交 ajax 表单并更改 options.url
【发布时间】:2016-02-19 11:28:11
【问题描述】:

我有带有 dropzonejs 插件的 Rails 应用程序来上传图像。 我正在上传图片之前创建Album。 然后我想上传图片到新相册。

Dropzone.options.myDropzone = {
  init: function() {
    this.on("processing", function(file) {
    # Performing album creation via $.ajax
    # $.ajax return ID of an album, but JS already skipped before I set new URL
    });
  }
};

我怎样才能等到请求完成并将返回的 ID 正确设置为 url 选项?

【问题讨论】:

    标签: javascript jquery ruby-on-rails ajax dropzone.js


    【解决方案1】:

    在这种情况下你可以使用accept -

    Dropzone.options.myDropzone = {
      accept: function(file, done)
            {
                file.postData = {};
                $.ajax({
                    url: '/album',
                    data: {name: file.name, type: file.type},
                    type: 'POST',
                    success: function(response)
                    {
                        file.album_id = response.album_id;
                        done();
                    },
                    error: function(response)
                    {
                        done('error preparing the file');
                    }
                });
            },
      init: function() {
        this.on("processing", function(file) {
          # Do other stuffs if needed
        });
      }
    };

    现在,Dropbox 将等到 drop() 被调用。

    【讨论】:

    • 这应该可以工作。我在我的项目中使用了这个。 :)
    猜你喜欢
    • 1970-01-01
    • 2019-07-06
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2014-11-29
    • 2011-10-31
    • 2020-04-29
    相关资源
    最近更新 更多