【问题标题】:Dropzone JS throws "undefined" when adding "error event"Dropzone JS 在添加“错误事件”时抛出“未定义”
【发布时间】:2016-07-14 15:49:18
【问题描述】:

我的 Dropzone 限制为 2 个文件 (maxFiles: 2)。如果用户将新文件拖入 Dropzone 中,maxfileexceeds 事件会显示错误。

myDropzone.on("maxfilesexceeded", function(file){
        alert("no more files accepted");
        myDropzone.removeFile(file);
    })

但是: 如果我添加“错误事件”..

myDropzone.on("error", function(file, errormessage, response){
        alert(response.message);
    })

为了在出现故障时得到响应,Dropzone 会发出“未定义”警报。 错误事件的参数应该是正确的.. Qoute(DropzoneJS主页):

错误:发生错误。接收 errorMessage 作为第二个参数,如果错误是由于 XMLHttpRequest 引起的,则将 xhr 对象作为第三个参数。

所以第一个参数是文件,第二个是错误消息(根据作者),第三个参数是来自服务器的错误。

服务器上的错误响应如下:

$response = array('status' => 'error', 'message' => 'unknown error occured');

header('HTTP/1.1 500 Internal Server Error');
header('Content-type: application/json');
$response["message"] = $message;
exit (json_encode($response));

那么为什么 Dropzone 给我一个“未定义”?

【问题讨论】:

    标签: javascript php html dropzone.js


    【解决方案1】:

    第三个参数是XHR 对象而不是响应。请试试这个:

    myDropzone.on("error", function(file, errormessage, xhr){
        if(xhr) {
            var response = JSON.parse(xhr.responseText);
            alert(response.message);
        }
    });
    

    【讨论】:

    • 感谢您的快速答复!现在可以了。认为只需传递 json 对象就足够了
    • 不客气!如果这解决了问题,请将其标记为已接受(以便将来遇到相同问题的人能够轻松发现它)。
    猜你喜欢
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多