Home > OS >  laravel - ajax formdata wont show in controller
laravel - ajax formdata wont show in controller

Time:07-04

I have tried many different combination for AJAX Setup in order to send the formData to controller. Somehow I cannot get the form input inside my controller despite trying to return it will all type of ways. May I know what did I miss that causing me not be able to get the input in controller?

$("#formCropUpdate").on("submit", function (event) {
        event.preventDefault();

        var formId = $('#formId').val();
        var url = '/Form/'   formId;
        var form = this;
        formData = new FormData(form);
        console.log(Array.from(formData));

        $.ajax({
            url: url,
            type: "PATCH",
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            cache: false,
            dataType: false,
            contentType: false,
            processData: false,
            data:formData,
            success: function (response) {
                console.log(response);
                return false;
            },
        });
    });
public function update(Request $request){
        $UserId = Auth::user()->id;
        $Company = Company::where('id', Auth::user()->company_id)->first();

        return $request->all();
}

CodePudding user response:

use

<meta name="csrf-token" content="{{ csrf_token() }}">

in head

and jQuery code

     $('#form_submit').submit(function (e) {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            e.preventDefault();
            var base = window.location.origin;
            let formData = new FormData(this);
            let  my_url = base   "/article-store";
            $.ajax({
                type: 'post',
                url: my_url,
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: (data) => {
           
                },
                error: function (data) {
                   
    
                }
            });
   });

CodePudding user response:

After quite a bit of digging, since what I am doing is using the PATCH request, it's still not working as of now with FormData. To solve it, we need to spoof the method by appending the PATCH method to formData and our AJAX settings to be changed to POST. Then you'll receive all the request inside your controller.

Reference: https://laracasts.com/discuss/channels/laravel/ajax-formdata-and-put-fails

https://laracasts.com/discuss/channels/javascript/axiosajax-http-patch-requests-with-file-not-working

$("#formCropUpdate").on("submit", function (event) {
        event.preventDefault();

        var formId = $('#formId').val();
        var url = '/Form/'   formId;
        var form = this;
        formData = new FormData(form);
        formData.append('_method', 'PATCH');
        console.log(Array.from(formData));

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $.ajax({
            type: "POST",
            url: url,
            data:formData,
            cache: false,
            contentType: false,
            processData: false,
            success: function (response) {
                console.log(response);
                return false;
            },
        });
    });
  • Related