2016-07-14 178 views
0

我的Dropzone限於2個文件(maxFiles:2)。如果用戶將新文件拖入Dropzone,maxfileexceeds事件顯示錯誤。Dropzone JS在添加「錯誤事件」時拋出「undefined」

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

但是: 如果我添加了 「錯誤事件」 ..

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

得到迴應,如果事情失敗,懸浮窗提醒 「未定義」。 上的錯誤事件的PARAMS應該是正確的.. 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.

所以第一個參數是(根據作者)的文件,第二的errormessage的並且是第三個參數是一個錯誤它來自服務器。

在服務器上的錯誤響應看起來是這樣的:

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

那麼爲什麼懸浮窗給我一個「未定義」?

回答

0

第三個參數是XHR對象沒有響應。請試試這個:

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

謝謝你的快速回答!它現在有效。認爲只要通過json對象就足夠了 – shiroxxs

+0

歡迎您!如果解決了這個問題,請將其標記爲已接受(這樣未來面對同樣問題的人將能夠輕鬆發現)。 –

相關問題