Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

ajax - How to call controller method with array

Controller import() Import data from csv file and redirect with array of data

//import data from csv file
return redirect('school/student/import')->with('result', $result);

import.blade.php This is a modal form and imported data is shown to user.

 @if(Session::has('result'))
 @php
    $result = Session::get('result');
 @endphp
    <table class="table table-hover">
       <thead>
           ...
            <tr>
                @foreach($result as $datarow)
                   <tr>
                        <td>{{$datarow['student_no']}}</th>
                        ....
                   </tr>
                @endforeach
           </tbody>
        </table>
@endif
<button type="submit" class="btn btn-primary">Save changes</button>

@section('js')
    <script>
        @if(Session::has('result'))
            //$result = IS IT POSSIBLE TO ACCESS $result;
            
            $('#exampleModalCenter').modal('show');

            $("form#myForm").submit(function(event) {
                event.preventDefault();
                $('#exampleModalCenter').modal('hide');
                $.ajax({
                    type: "POST",
                    url: "savedata",
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    data: **$result**,
                    success: function(data){
                        window.location = '{{ url("school/student") }}';
                    },
                });
            });
        @endif
    </script>
@stop

Once submit is clicked, I want to call Controller savedata() with $result data array. I tried with both form submit and ajax post but the issue is since $result is an array, it's difficult to pass. Also unable to access session values from JavaScript.

question from:https://stackoverflow.com/questions/66056902/how-to-call-controller-method-with-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Need to provide the route name

routes/web.php

Route::post('savedata', 'ExampleController@savedata')->name('example.savedata');

After naming the route, you need to provide the route name to the URL for ajax.

$("form#myForm").submit(function(event) {
    event.preventDefault();
    $('#exampleModalCenter').modal('hide');
    $.ajax({
        type: "POST",
        url: '{{route("example.savedata")}}',
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        data: $result,
        success: function(data){
            window.location = '{{ url("school/student") }}';
        },
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...