How to correctly receive ajax's post data?

Posted 4 months ago by AlexanderKim

Here's ajax:

            $.upload = function(file) {
                let out = new FormData();
                out.append('file', file, file.name);

                $.ajax({
                    method: 'POST',
                    url: '/summer-uploads',
                    //check laravel document: https://laravel.com/docs/5.6/csrf#csrf-x-csrf-token
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    contentType: false,
                    cache: false,
                    processData: false,
                    dataType: 'JSON',
                    data: out,
                    success: function(url) {
                        console.log(url);
                        $('#description').summernote('insertImage', url);
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        console.error(textStatus + " " + errorThrown);
                    }
                });
            };

Controller:

    public function summerUploads(Request $request)
    {
        if ($request->ajax()) {
            $file = $request->input('file');

            return response()->json($file->path());
        }

        return App::abort(404);
    }

But i'm keep getting an error: "Call to a member function path() on null"

I guess, it's not getting the values from ajax's data? Because $file is null.

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.