Laravel and Ajax to obtain data

1

I hope to explain my best so that you can give me a little help: S

I have the following JavaScript code:

 $.ajax({
url: "{{ route('comprobar') }}",
method: 'get',
dataType: 'json',
success: function(respuesta) {
  console.log("información");

},
error: function() {
  console.log("No se ha podido obtener la información");
}});

And the following codes correspond to the route of Laravel and its controller:

Route::get('comprobar',[
'as'=>'comprobar',
'uses'=> 'CrearPersonajeController@comprobar']);


function comprobar()
{
    $idUsuario = \Auth::user()->id;
    $respuesta= DB::select("SELECT * FROM partidas WHERE idUsuario = " . $idUsuario );
    dd($respuesta);

    return Response::json(array());
}

The problem that occurs to me is that it always enters the part of: It has not been possible to obtain information and I can not think of what can be a solution since what I want is to extract data from the database with Ajax to do a couple of checks on a form with steps (steps), I also add that I get an error by the browser console: jquery.min.js: 4 GET link 500 (Internal Server Error). Thank you very much for your time.

I have been able to improve the javascript / jquery code so that it returns true or false in case I do not find what I need but my problem now is that this function does not return anything to the variable I assigned it.

 var nickPartida = document.getElementById('nickPartida').value;
 var devuelta = comprobarNickPartida(nickPartida);
 //aqui me duelve indefinido cuando en la función principal devuelve true
 console.log(devuelta);

function comprobarNickPartida(nickPartida)
{     
  var devolver = false;
   $(document).ready(function(){
      //Comprobación del nombre mediante Ajax
      $.ajax({
      url: "{{ route('comprobar') }}",
      method: 'get',
      dataType: 'json',
      success: function(response) {
        //Comprobar que el nick existe en el array o si el tamaño 
        //del array es mas de uno para decirle al usuario que ya existe
       
        for (let index = 0; index < response.length; index++) 
        { 
          if(response[index]['nickPartida'] == nickPartida)
          {
            devolver = true;
          }
        }
        return devolver;
      },
      error: function() {
        console.log("No se ha podido obtener la información");
      }
    });
   })

   return devolver;
}
    
asked by Miguel C 10.11.2018 в 17:20
source

2 answers

0

Ajax errors can not be seen in the browser console, when a server-side error occurs, the only information you will get is that, 500 (Internal Server Error). To see the real error you must go to the Network section of the developer tools, while you are there, perform the action sent by the ajax and you will see the real error.

    
answered by 11.11.2018 в 05:25
0

Well after two full days stuck with the code to do it with Ajax, I chose to do it via the Laravel driver and to see it and check it from there. He left the code so you can see it more clearly.

 function index(){
    $idUsuario = \Auth::user()->id;
    $response= DB::select("SELECT nickPartida FROM partidas WHERE idUsuario = " . $idUsuario );

    return view('crearpersonaje',compact('response'));
}

From there I see it and work with it in the verification directly:

<script> var arrayNombres = <?php echo(json_encode($response)); ?></script>
<script src="{{asset('js/propio.js')}}"></script>
<script>
 var inputNickPartida = document.getElementById('nickPartida');
 var existeNick = false;
 for (let index = 0; index < arrayNombres.length; index++) {
  if(arrayNombres[index]['nickPartida'] == inputNickPartida.value){
    console.log("existe");
    existeNick = true;
    document.getElementById('nickPartida').setAttribute('class',inputNickPartida.className + " invalid");
    }
  }
</script>

Thanks for the help anyway and I hope I can help with any questions you may have.

    
answered by 12.11.2018 в 13:19