【问题标题】:jquery ajax file upload, success doesn't get calledjquery ajax文件上传,成功不会被调用
【发布时间】:2017-04-01 02:31:21
【问题描述】:

我正在使用 jquery 将图像上传到 joomla 站点,但文件 ajaxfileupload.js 有问题。图片上传得很好,但“成功”中的函数没有被调用。我需要它来显示上传完成的确认。我尝试使用“完成”而不是“成功”,然后代码可以工作,但它会一遍又一遍地刷新页面。我怎样才能让它像它应该的那样工作?这是代码..

upload: function (a) {
    if (jQuery("#Filedata-" + a).val() == "") {
        jQuery("#photoupload-" + a).remove();
        joms.uploader.upload();
        if (jQuery("#photoupload").next().length == 0) {
            joms.uploader.addNewUpload()
        }
        jQuery("#upload-photos-button").show();
        return
    }
    var c = (jQuery("#photoupload-" + a + " :input:checked").val() == "1") ? "&defaultphoto=1" : "";
    var b = jQuery("#photoupload-" + a).next().find(".elementIndex").val();
    b = (b != "") ? "&nextupload=" + b : "";
    jQuery("#photoupload-" + a).children().each(function () {
        jQuery(this).css("display", "none")
    });
    jQuery("#photoupload-" + a).append('<div id="photoupload-loading-' + a + '"><span class="loading" style="display:block;float: none;margin: 0px;"></span><span>' + joms.uploader.uploadText + "</span></div>");
    jQuery.ajaxFileUpload({
        url: this.postUrl + c + b,
        secureuri: false,
        fileElementId: "Filedata-" + a,
        dataType: "json",
        success: function (e, d) {
            jQuery("#photoupload-loading-" + a).remove();
            if (typeof (e.error) != "undefined" && e.error == "true") {
                jQuery("#photoupload-" + a).css("background", "#ffeded");
                jQuery("#photoupload-" + a).append('<span class="error">' + e.msg + "</span>")
            } else {
                jQuery("#photoupload-" + a).css("background", "#edfff3");
                jQuery("#photoupload-" + a).append('<span class="success">' + e.msg + "</span>")
            }
            jQuery("#photoupload-" + a).fadeOut(4500, function () {
                jQuery("#photoupload-" + a).remove();
                if (jQuery("#photoupload").next().length == 0) {
                    joms.uploader.addNewUpload()
                }
            });
            jQuery("#photoupload-" + a + " .remove").css("display", "block");
            if (e.nextupload != "undefined") {
                joms.uploader.upload(e.nextupload);
                return
            } else {
                jQuery("#upload-photos-button").show()
            }
        },
        error: function (f, d, g) {}
    });
    return false
}

【问题讨论】:

  • 错误是什么。您可以使用 error:function 来获取错误。
  • 能不能把服务器发过来的JSON贴一下?
  • 只提示错误信息并检查
  • 警报显示以下错误消息:SyntaxError: Unexpected token :
  • JSON 响应为:{msg: '照片上传成功。',nextupload: '2'}

标签: javascript jquery ajax file-upload


【解决方案1】:

我想我解决了这个问题。您必须删除选项:数据,数据类型。它会正常工作的。

【讨论】:

  • 也为我工作。我不喜欢它,但我会接受它。
【解决方案2】:

虽然这可能已经晚了,但对于第一次看到这个的人来说只是改变 数据类型:“json”, 至 数据类型:“JSON”,

【讨论】:

  • 这样做开辟了一个全新的蠕虫罐。
猜你喜欢
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
相关资源
最近更新 更多