【问题标题】:How to alert if there is a Dropzone error?如果出现 Dropzone 错误,如何发出警报?
【发布时间】:2018-05-31 09:28:24
【问题描述】:

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

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

});

【问题讨论】:

  • 需要返回错误http状态码:return new JsonResponse(['reason' => 'Some Reason'], 400);
  • 您能否将解决方案发布为答案
  • 你使用的是什么版本的 Laravel?另外,您如何进行验证?你能展示整个控制器方法吗?

标签: php laravel dropzone.js


【解决方案1】:

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

当你这样做时

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

Laravel 默认会返回 200 状态码。

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

return new JsonResponse([], 400);

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

【讨论】:

  • 非常感谢先生,我为这个简单的解决方案浪费了很多时间
猜你喜欢
  • 2023-03-13
  • 2010-11-27
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多