samedi 26 octobre 2019

How to solve an Ajax Data error in PHP 500?

I have created an app with a form that register form that uses ajax to save, I'm getting a server error at the moment to validate or trying to save data.

Bellow please find the JS code used for the Ajax

guardar_usuario :function(e){
  //function guardar_cliente(e){
    console.log(e);
    //debugger;
    console.log("guardar_usuario :function1");
    //console.log(e);



    var residente_name = $("#u-residente-input").val();
    var frac = $("#u-fraccionamiento-input").val();
    var calle = $("#u-calle-input").val();
    var numero = $("#u-numero-input").val();
    var email = $("#u-email-input").val();
    var Password = $("#u-pass-input").val();
    var Rol = $("#u-rol-input :selected").val();
    var telefono = $("#u-tel-input").val();

    var token = $("#csrf-token").val();
    //  console.log($("#residente-select :selected").text());
    //  console.log($("#periodo-select :selected").val());

    var id = $(e).data("id");

    console.log(token);

    if (id!=="undefined"){
      id = '';

    }

    console.log(id);
    console.log("ID antes");

      if(residente_name == ""){
        $("#u-residente-input").css("border","1px solid red");
        alert("El capo de residente no puede ir vacio");
          //$("#name-input").css("border","0px solid red");
      }else if (email=="") {
        $("#u-email-input").css("border","1px solid red");
        alert("El capo de email no puede ir vacio");
        //  $("#email-input").css("border","0px solid red");
      }else if (Password=="") {
        $("#u-pass-input").css("border","2px solid red");
        alert("El capo de Password no puede ir vacio");
      //  $("#pass-input").css("border","0px solid red");
      }else {
              // console.log(name);
              // console.log(email);x
              // console.log(pass);
              var data={'_token':token,
                        'residente_name':residente_name,
                        'frac':frac,
                        'calle':calle,
                        'numero':numero,
                        'email':email,
                        'Password':Password,
                        'Rol':Rol,
                        'telefono':telefono,
                        'id':id
                        };
              console.log("datos 1");          
              console.log(data);
              $.ajax({//sintaxis para AJAX esto manda al server las variables y asi el controlador las maneja y ve lo que responde el server
                url:main_path+"/residente/store",//el main pad viene de linea 9 de la vista.blade.php
                data:data, // valores en la variable data
                type:"post",
                datatype:"json",
                success:function(serv){
                  debugger;
                  console.log(data);
                  console.log(serv.status);
                  if(!serv.status){
                    //construir the err mesg
                    var error = "";

                    console.log("err");
                    $.each(serv.message, function(i,val){
                      error += val+",132 ";
                    });

                    alert(serv.message);
                    //alert(error);
                  }else{
                    console.log("next");
                    alert(serv.message + " " + residente_name);
                    $.each($(".modal-body").find("input"),function(){
                            $(this).val("");
                          });
                    $("#mantenimiento-modal").modal("hide");
                    window.location.href = main_path+"/residente";
                  }
                },
                cancel:function(serv){
                  console.log(serv);
                }
              });
            }
        //}
  }

bellow is the controller code, i've been testing and the error is happening in the controller, at the moment to validate or save data the error appear.

public function store(Request $request)// Request palabra recervada
  {
    # code...
    $data = $request -> all();//trae todas las variables

    $validacion = \Validator::make($data,
      array(
            'email'=> 'required|unique:Usuario'//|unique'

          ),
      array(
            'email.required' => "El periodo es requerido"
            ,'email.unique' => "Ya Existe un record con este email"

            )// parametros 2 de Validator
    );
    };

     if($validacion->fails()){
       return response() ->json(array('status' => false, 'message' => $validacion->messages()));
     }

     $usuario = new Mantenimiento();


    $usuario ->residente = $data['residente_name'];
    $usuario ->fraccionamiento = 'frac';
    $usuario ->calle = $data['calle'];
    $usuario ->numero = $data['numero'];
    $usuario ->email = $data['email'];
    $usuario ->password = bcrypt($data['Password']);
    $usuario ->rol_id = $data['Rol'];
    $usuario ->telefono = $data['telefono'];
    $usuario ->bit_activo = 1;
    $usuario -> save();

    return response()->json(array('status' => true, 'message'=>'Cliente Guardado', 'data'=> $data ));

  }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2BKkDcG
via IFTTT

Aucun commentaire:

Enregistrer un commentaire