dropzone 文件上传成功后,无法显示从 laravel 控制器发送的额外 json 数据

Unable to display additional json data sent from laravel controller after dropzone file upload success

在成功上传 dropzone js 文件并将其作为 dropzone js 成功事件发回至客户。

如果我这样写 laravel 控制器

return response()->json(array('success' => true, 'message' => 'custom success message!', 'additional' => 'info'), 200);

并在 我的客户端 中调用响应,如下所示:

<script>
    Dropzone.autoDiscover = false;
    $(document).ready(function() {
        var myDrop = new Dropzone("div#divdropzone", {

            //..edited the rest of the script..

            init: function() {
                this.on("success", function(file, response) {
                    console.log(response);
                });
            }
        });
    });
</script>

它会在我的浏览器控制台日志中给出这个错误:

Invalid JSON response from server.

如果我从我的 laravel 控制器中删除 return response()->json(没有 returning任何东西),这是 console.log(响应);

ca0ba5acc1c71f922278ea9f6ad7fa4e.jpeg
jpg
C:\Windows\Temp\php83D1.tmp

仅供参考,文件上传工作正常,因此有或没有

return response()->json(array('success' => true, 'message' => 'custom success message!', 'additional' => 'info'), 200);

我试图对来自服务器的 json 响应做的是查看我的成功消息以及附加数据到客户端的 console.log() 以便我可以进一步处理响应例如:

init: function() {
    this.on("success", function(file, response) {
        console.log(response.message);
        console.log(response.success);
        console.log(response.additional);
    });
}

我仍然无法弄清楚我的服务器的 json 数据如何与 dropzone on("success") 响应参数通信。 请帮我指出我的 laravel 控制器或客户端 dropzone 脚本中的错误。任何帮助将非常感激。谢谢。

我刚刚搞定了一些东西并让它工作了。这是 Laravel 5.4.23 和最新的 dropzone.js
我的问题基本上是我在控制器内部仍然有一个 DIEDUMP (dd) 来进行一些调试。

控制器/图像处理器

// whatever logic here
return response()->json(['success' => true, 'payload' => 'My message']);

然后在我的 JS 中:

Dropzone.options.myAwesomeDropzone = {
    uploadMultiple: false,
    parallelUploads: true,
    maxFilesize: "{{ dsoptions.maxfilesize }}",
    // whatever other options you got here
    init:function() {
        // .on event handlers see dropzone docs for info
    },
    error:function(file, response) {
        // error handling
    },
    success:function(file, response) {
        console.log(response.payload);
    }
}

就这么简单。因此,如果我的响应中的 success 是错误的,我现在可以检查并相应地调整 dropzone success 函数中的消息。