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;
}