首页 > 解决方案 > 如果出现 Dropzone 错误,如何发出警报?

问题描述

这是我的控制器,我返回错误

if(!empty($chckDocsDup)){
     $return['error'] = 1;
     return response()->json($return);
     exit();
 }

在我的 Dropzone 中,我遇到了这些错误,this.on('error', function())但它没有发出警报。有什么替代的想法吗?

$(document).ready(function () {
    var uplodURLother = '{{url(ADMIN."/uploaddocument")}}';
    var metaTokenOther = $('meta[name="_token"]').attr('content');
    var uploaddoctypeOther = $('#uploaddoctype_other').val();
    Dropzone.options.myDropOther = {
    acceptedFiles: ".pdf,.doc,.docx",
    url: uplodURLother,
    sending: function(file, xhr, formData) {
        formData.append("_token", metaTokenOther);
        formData.append("doctype", uploaddoctypeOther);
        formData.append("candidateid", '{{$candidateDet['candidateid'] }}');
    },
      init: function() {
         this.on("removedfile", function(file) {
            $(".remv_"+file.doctype+"_"+file.filecnt).remove();
        });
        this.on('success', function(file, response) {
                alert('hai');
          });   
            this.on('error', function(file, response) {
               alert('response'); //This alert is not working
            });
      }
    };

});

标签: phplaraveldropzone.js

解决方案


Dropzone 通过查看响应的状态代码来检查上传是否失败。

当你这样做

return response()->json([]);

Laravel 默认会返回 200 状态码。

为了返回错误状态,您需要执行类似的操作

return new JsonResponse([], 400);

您可以更改400为任何4xx状态代码 (IE 422)


推荐阅读