ERROR: POST.... 500 (Internal Server Error) - LARAVEL and AJAX

Asked

Viewed 1,590 times

0

I am facing a problem when returning a View to another View using Ajax.

I am presented with the following error on the Google Chrome console:

jquery.min.js:2 POST http://localhost:8000/tbody 500 (Internal Server Error)
send                    @   jquery.min.js:2
ajax                    @   jquery.min.js:2
w.fn.load               @   jquery.min.js:2
(anonymous)             @   people:578
u                       @   jquery.min.js:2
fireWith                @   jquery.min.js:2
k                       @   jquery.min.js:2
(anonymous)             @   jquery.min.js:2
load (async)        
send                    @   jquery.min.js:2
ajax                    @   jquery.min.js:2
w.(anonymous function)  @   jquery.min.js:2
attIndexSearch          @   people:575
onclick                 @   people:275

This is my Java code where I call the other view: This code is found in View Peoples/index.blade.php

<script type="text/javascript">

    function attIndexSearch (){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.get('/peoples/indexSearch', function (peoples){
            document.getElementById("tbody").innerHTML = "";
            var search = $("#tbody").val();
            $("#tbody").load("tbody", {peoples:peoples});
        }) 
    }
</script>

This is the route to call the view tbody:

Route::post('tbody', ['as' => 'peoples.tbody', 'uses' => 'PeopleController@tbody']);
Route::get('/peoples/indexSearch', ['as' => 'peoples.indexSearch', 'uses' => 'PeopleController@indexSearch']);

This is the tbody function code in the controller:

public function tbody()
{
    return view('peoples.tbody');
}

This is the code of the Indexsearch function in the controller:

public function indexSearch(People $model, Address $modelA, Request $request)
    {
        $nameOrcpf = $request->nameOrcpf;
        $type_person = $request->type_person;
        if ($nameOrcpf == null) $nameOrcpf = '';
        if ($type_person == null) $type_person = '';
        if ($type_person != ''){
            $model = $model::where('type_person', 'like', $type_person)->
                    where('name', 'like', '%'.$nameOrcpf.'%')->
                    orWhere('cpf', 'like', $nameOrcpf)->
                    orderBy('name')->
                    paginate(15); 
            return response()->json($model);   
        } else {
           $model = $model::where('name', 'like', '%'.$nameOrcpf.'%')->
                   orWhere('cpf', 'like', $nameOrcpf)->
                   orderBy('name')->
                   paginate(15);
            return response()->json($model);   
        }
    }

This is the code of my view that is called, which is found in Peoples/tbody.blade.php.

@csrf
@foreach ($peoples as $people)

<tr>
    <td style="white-space: pre-wrap;">{{ $people->name }}</td>
    <td>{{ $people->cpf }}</td>
    <td>
        <a href="mailto:{{ $people->email }}">{{ $people->email }}</a>
    </td>
    <td>{{ $people->tel }}</td>
    <td> 
        @if ($people->type_person == "A" and $people->sex == "M") Sócio
        @endif
        @if ($people->type_person == "A" and $people->sex == "F") Sócia
        @endif
        @if ($people->type_person == "D" and $people->sex == "M") Doador
        @endif
        @if ($people->type_person == "D" and $people->sex == "F") Doadora
        @endif
    </td>
    <td class="text-right">
        <div class="dropdown">
            <a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="fas fa-ellipsis-v"></i>
            </a>
            <div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">

                <a class="dropdown-item" href="{{ route('peoples.view', $people) }}">{{ __('Ver') }}
                </a>  

                <a class="dropdown-item" href="{{ route('peoples.edit', $people) }}">{{ __('Editar') }}
                </a>   

                <button type="button" class="dropdown-item" data-toggle="modal" data-target="#modal-notification" onClick="javascript: GetId({{$people->id}},'{{$people->name}}');">Excluir</button>


            </div>
        </div>
    </td>
</tr>
@endforeach

</tbody>

Important remarks

Everything works until you get to View tbody. How do I know that? If in the view tbody, I put only one name, for example, 'I’m here', no error is displayed and the view is successfully returned. If I put the code <?php var_dump($_POST['peoples']); ?> Also no error is returned to me, and the view works by passing the information. So whenever I try to use $peoples just as object is returned me the error.

1 answer

2


You need to pass the value of $peoples to the View.

public function tbody()
{
    $peoples = People::all(); // Exemplo se for pegar os dados do banco.
    return view('peoples.tbody', ['peoples' => $peoples]);
}
  • Hey, buddy, it kind of worked. The only problem is that in this case will fetch data again in the Bank and I wanted to return the data I passed on $("#tbody").load("tbody", {peoples:peoples}); of this variable Peoples; which comes from this function indexSearch, I just added this function to the question so you can see.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.