Dropzone JS 添加“错误事件”时抛出“未定义”

shi*_*xxs 2 html javascript php dropzone.js

我的 Dropzone 仅限于 2 个文件 ( maxFiles: 2)。如果用户将新文件拖动到 Dropzone 中,maxfileexceeds 事件将显示错误。

myDropzone.on("maxfilesexceeded", function(file){
        alert("no more files accepted");
        myDropzone.removeFile(file);
    })
Run Code Online (Sandbox Code Playgroud)

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

myDropzone.on("error", function(file, errormessage, response){
        alert(response.message);
    })
Run Code Online (Sandbox Code Playgroud)

为了在出现故障时获得响应,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));
Run Code Online (Sandbox Code Playgroud)

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

Ism*_*OUH 7

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

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