添加 "error event" 时 Dropzone JS 抛出 "undefined"

Dropzone JS throws "undefined" when adding "error event"

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

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

但是: 如果我添加 "error event"..

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

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

error: An error occured. Receives the errorMessage as second parameter and if the error was due to the XMLHttpRequest the xhr object as third.

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

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

$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 给我一个 "undefined"?

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

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