【问题标题】:jQuery file upload plugin - fail callback not firing in IE < 10jQuery 文件上传插件 - 在 IE < 10 中未触发失败回调
【发布时间】:2014-01-17 02:48:23
【问题描述】:

我正在使用https://github.com/blueimp/jQuery-File-Upload

它在现代浏览器中使用 XHR 文件上传,在 oldIE (8,9) 中使用隐藏 iframe 上传。

当文件验证失败时,服务器返回一个 4xx 状态码(错误/失败)。

$('#fileupload').fileupload({
    url: url,
    dataType: 'json',
    done: function (e, data) {
       console.log("done")
    },
    fail: function (e, data) {
       console.log("fail")
    }
})

但是,在 oldIE 中,即使我发送 4xx 状态代码,也会调用 done 回调。 有没有办法让它调用失败回调(https://github.com/blueimp/jQuery-File-Upload/wiki/Options#fail)并读取响应正文或状态码?

我认为这可能与上传文件的隐藏 iframe 方法有关,但我在文档中找不到任何具体内容。

【问题讨论】:

标签: jquery iframe callback jquery-file-upload


【解决方案1】:

我应该检查一下Frequently Asked Questions

// By default, the iFrame Transport handles all responses as success,
// so we override the iFrame to JSON converter to handle error responses:

$.ajaxSetup({
    converters: {
    'iframe json': function (iframe) {
        var result = iframe && $.parseJSON($(iframe[0].body).text());
        if (result.error) {
            // Handling JSON responses with error property:
            // {"error": "Upload failed"}
            throw result.error;
        }
        return result;
    }
  }
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 2013-03-20
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多