Laravel always return invalid json response when using Dropzone

69 Views Asked by At

I have tried using Dropzone with laravel. And follow the tutorial at this link:here

The file was uploaded successfully, but the json response from the controller is always invalid. After I checked deeper in the inspect element network. always returns:

<br />
<b>Notice</b>:  PHP Request Startup: file created in the system's temporary directory in <b>Unknown</b> on line <b>0</b><br />
{"name":"testing","original_name":"wakanda"}

so it always produces:Invalid JSON response from server.

This is my controller, I commented the line to upload the file because I want to see a direct response from the json. and the result is the same:

public function storeMedia(Request $request)
    {
//        $path = storage_path('tmp/uploads');
//        if (!file_exists($path)) {
//            mkdir($path, 0777, true);
//        }
//        $file = $request->file('file');
//        $name = uniqid() . '_' . trim($file->getClientOriginalName());
//        $file->move($path, $name);
        return response()->json([
            'name' => "testing",
            'original_name' => "wakanda"
        ], 200);
    }

my view:

<div class="form-group">
                                    <label for="document">Documents</label>
                                    <div class="needsclick dropzone" id="my-dropzone">
                                    </div>
                                </div>



var uploadedDocumentMap = {}
                Dropzone.options.myDropzone = {
                    url: 'http://127.0.0.1:8000/api/ajax/products/gambar/',
                    // maxFilesize: 2, // MB
                    addRemoveLinks: true,
                    method : 'POST',
                    {{--headers: {--}}
                    {{--    'X-CSRF-TOKEN': "{{ csrf_token() }}"--}}
                    {{--},--}}
                  
                    success: function (file,response) {
                        $('form').append('<input type="hidden" name="image[]" value="' + response.name + '">');
                        // uploadedDocumentMap[file.name] = response.name
                        console.log(response);
                    },

                    removedfile: function (file) {
                        // console.log(file);
                        file.previewElement.remove()
                        var name = ''
                        if (typeof file.file_name !== 'undefined') {
                            name = file.file_name
                        } else {
                            name = uploadedDocumentMap[file.name]
                        }
                        $('form').find('input[name="image[]"][value="' + name + '"]').remove();
                    },
                    {{--init: function () {--}}
                    {{--    @if(isset($products) && $products->document)--}}
                    {{--    var files =--}}
                    {{--        {!! json_encode($project->document) !!}--}}
                    {{--        for (var i in files) {--}}
                    {{--        var file = files[i]--}}
                    {{--        this.options.addedfile.call(this, file)--}}
                    {{--        file.previewElement.classList.add('dz-complete')--}}
                    {{--        $('form').append('<input type="hidden" name="document[]" value="' + file.file_name + '">')--}}
                    {{--    }--}}
                    {{--    @endif--}}
                    {{--}--}}
                }
1

There are 1 best solutions below

4
Niraj Shah On

You can disable notices by setting error reporting level to E_ALL & ~E_NOTICE; using either error_reporting in the php.ini file or by using the error_reporting() function.

You can also fix the notice by checking the upload_tmp_dir setting in the php.ini file. The notice may be telling you that the path set is incorrect.