Home > other >  Pass data with ajax in laravel, it does not detect my method
Pass data with ajax in laravel, it does not detect my method

Time:07-20

I am trying to pass records through a checkbox using ajax and jquery, the problem is that when executing my function it returns an empty array, at the moment I am testing but it does not let me advance. my script

 <script>
         $(document).ready(function(){
               $('.save_btn').on('click',function(e){
                   e.preventDefault();
                   const prodid= [];
                   const prodname = [];
                   $('.prod-id').each(function(){
                       if($(this).is(":checked")){
                           prodid.push($(this).val());
                       }
                   });
                   $('input[name^="prodname"]').each(function(){
                      prodname.push($(this).val());
                   });
                   $.ajax({
                     url:'{{ route('save_data') }}',
                     type: 'POST',
                     data: {
                      "_token": "{{ csrf_token() }}",
                      prodid: prodid
                     },
                     success:function(response){
                     }
                   });
           });
           });
       </script>

At the moment I am only passing a parameter of an input for the test but it is not being passed inside the array.

    <tbody>
                               @foreach($ventas1 as $ventas)
                               <tr>
                                   <td>
                                      <input type="checkbox" name="prodid" id="prod-id" value="mobile">
                                   </td>
                                   <td> {{ date("d-m-Y",strtotime($ventas->FECHA)) }}</td>
                                <td>   <input name="prodname[]"  id="moneda" value={{$ventas->MONEDA}} readonly> </td>
                                {{--   <td><input type="text">{{$ventas->MONEDA}}</td> --}}
                                   <td>{{$ventas->NUMCTA}}</td>
                                   <td>{{$ventas->CONCEPTO}}</td>
                                   <td>{{$ventas->FACTURA}}</td>
                                   <td>{{$ventas->DENOMINACION_SOCIAL}}</td>
                                   <td>{{$ventas->VENDEDOR}}</td>
                                   <td>${{$ventas->IMPORTE}}</td>
                                   {{-- <td>${{$ventas->IMPORTEEXT}}</td> --}}
                               </tr>
                               @endforeach
                           </tbody>

This is my route where I am passing the data to the function in my controller Route::get('save_data',[ventas1Controller::class,'save_data'])->name('save_data'); And this is my controller, I am using a dd to be able to see the array with the data that I am passing but it is not passing said data

 public function save_data(Request $request) {
       dd($request->all());
   }

CodePudding user response:

You route is expecting GET method but you are requesting POST method from your AJAX request as a result method is not found. Try the below script;

<script>
    $(document).ready(function(){
        $('.save_btn').on('click',function(e){
            e.preventDefault();
            const prodid= [];
            const prodname = [];
            $('.prod-id').each(function(){
                if($(this).is(":checked")){
                    prodid.push($(this).val());
                }
            });
            $('input[name^="prodname"]').each(function(){
                prodname.push($(this).val());
            });
            $.ajax({
                url:`{{ route('save_data') }}`,
                type: 'GET',
                data: {
                "_token": "{{ csrf_token() }}",
                    prodid: prodid
                },
                success:function(response){
                    
                }
            });
        });
    });
</script>

CodePudding user response:

This is the screen maybe I could better express my mistake enter image description here

  • Related