dqf2015
2016-07-14 15:49
浏览 115

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

My Dropzone is limited to 2 Files ( maxFiles: 2). If the user drags a new File into the Dropzone, the maxfileexceeds event shows an error.

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

But: If I add the "error event"..

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

to get a response if something fails, Dropzone alerts an "undefined". The params on the error event should be correct.. Qoute(DropzoneJS homepage):

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

So the first param is the file, second an errormessage (according to the author) and the 3rd param is a error which comes from the server.

The error response on the server looks like this:

$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));

so why does Dropzone give me an "undefined" ?

图片转代码服务由CSDN问答提供 功能建议

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

  myDropzone.on(“maxfilesexceeded”,function(file){
 alert(“ 不再接受文件“); 
 myDropzone.removeFile(文件); 
})
   
 
 

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

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

如果出现故障则得到响应,Dropzone警告“未定义”。 错误事件的参数应该是正确的.. Qoute(DropzoneJS主页): \ n

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

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

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

  $ response = array('status'=>'error','message'  =>'未知错误发生'); 
 
header('HTTP / 1.1 500内部服务器错误'); 
header('Content-type:application / json'); 
 $ response [“message”] =  $ message; 
exit(json_encode($ response)); 
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglie9067 2016-07-14 15:54
    已采纳

    The third parameter is an XHR object not the response. Please try this:

    myDropzone.on("error", function(file, errormessage, xhr){
        if(xhr) {
            var response = JSON.parse(xhr.responseText);
            alert(response.message);
        }
    });
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题