Ajax search with Laravel 5.6 and jquery

Asked

Viewed 386 times

1

I’m having trouble executing an ajax search with Laravel, follow the ajax code of my view:

 $(document).ready(function () {
  $('#enviarForm').click(function (e) { 
    e.preventDefault();
    var form = $('#filtro').serialize();

    $.ajaxSetup({
              headers: {
                  'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
              }
          });
          $.ajax({
            type: "get",
            url: "{{url('/cadastro')}}",
            data: form,
            dataType: "json",
            success: function (response) {
              alert('ola');
            }
          });
  });
});

Controller code:

if($request->ajax()){
        $nome = $request['ds_paciente'];
    $Pessoas=\App\Pessoas::where('ID_CD_TIPOPESSOA',1)
                           ->where('IN_STATUS',1)
                           ->where('NM_PESSOA', 'LIKE', '%' . $nome . '%')
                            ->paginate(10);

    return view('todosPacientes',compact('Pessoas'));
    }

I’m a little lost and I’m not finding tutorials on the internet.

  • Do you have a mistake? Or what happens? The form #filtro has the field ds_paciente?

  • The mistake is that nothing happens, it just doesn’t work, the fields are correct

  • Post your html/Blade code as well

  • tried $request->input('ds_paciente')

  • Bro, first if you want return using Ajax, the controller has to be: Return Response::json($Ponsulta);

No answers

Browser other questions tagged

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