Ajax does not bring me the data

2

I tell you my problem I'm trying to edit a record and the data I send to call through an id with Js and it works.

But after sending that id with Ajax through "POST" to a method to do the query and return the values, it sends me an error in the method, and the error is in "if" but I still do not understand why.

And by the way, when sending the data by ajax, I had dataType:"json" but I commented it because it did not show me anything.

I leave the code so you can help me ...

users.js (Js and Ajax):

$(document).on("click", ".btnEditarUsuario", function(){

   var idUsuario = $(this).attr("idUsuario");
   console.log("idUsuario", idUsuario); //Mandando llamar el id con la consola

   //AJAX
   var datos = new FormData();
   datos.append("idUsuario", idUsuario);

  $.ajax({

       url:"../ajax/usuarios.ajax.php",
       method: "POST",
       data: datos,
       cache: false,
       contentType: false,
       processData: false,
       //dataType: "json",
       success: function(respuesta){

           console.log("respuesta", respuesta);

       }

   });  })

The result in the console:

And finally in the if at the end is where is the supposed error that appears in the console, only what I do is through the method to consult the data with the id that I send from the post of Ajax and here execute it.

require_once "../controladores/usuarios.controlador.php";
require_once "../modelos/usuarios.modelo.php";

class AjaxUsuarios{

/*=============================================
EDITAR USUARIO
=============================================*/ 

public $idUsuario;

public function ajaxEditarUsuario(){

    $item = "idusuario";
    $valor = $this->idUsuario;

    $respuesta = ControladorUsuarios::ctrMostrarUsuarios($item, $valor);


    echo json_encode($respuesta);

}//Cierra funcion

   }//Cierra clase
// EDITAR USUARIO AQUI ES EL ERROR

if(isset($_POST["idUsuario"])){

$editar = new AjaxUsuarios();
$editar -> idUsuario = $_POST["idUsuario"];
$editar -> ajaxEditarUsuario();
}  

This is the class I'm calling with the HTML:

<div class="btn-group">
   <button class="btn btn-warning btnEditarUsuario"  
    idUsuario="'.$value["idusuario"].'" data-toggle="modal" 
    data-target="#modalEditarUsuario"><i class="fa fa-pencil"></i></button>

   <button class="btn btn-danger"><i class="fa fa-times"></i></button>

 </div> 

What now shows me in console are the data I need highlighted in red, the rest is HTML:

RESOLVED UPDATE

Console, the data I expected to obtain:

JS Code:

Class:

    
asked by Jonathan 21.04.2018 в 18:15
source

2 answers

2

Try these corrections:

PHP

The if is not part of the class. Therefore, the class must close at the end of the function ajaxEditarUsuario .

In any case, it is a code that should be separated, always having your class separate.

/*
   *Si alguno de estos dos archivos del require sacan algo por pantalla 
   *tu petición Ajax no funcionará de la manera esperada. 
   *Las peticiones Ajax se suelen hacer para obtener cosas muy concretas
   *cualquier otra cosa que salga por pantalla, aparte de lo que se está pidiendo
   *por ejemplo un mensaje de error, contenido HTML, un 'echo, print' u otro
   *hará que el código devuelva otros datos que no son los esperados
   *es de suma importancia controlar eso
*/


require_once "../controladores/usuarios.controlador.php";
require_once "../modelos/usuarios.modelo.php";

class AjaxUsuarios{

/*=============================================
EDITAR USUARIO
=============================================*/ 

    public $idUsuario;

    public function ajaxEditarUsuario(){

        $item = "idusuario";
        $valor = $this->idUsuario;

        $respuesta = ControladorUsuarios::ctrMostrarUsuarios($item, $valor);


        echo json_encode($respuesta);

    }
/*Cierre de la clase*/
} 

// EDITAR USUARIO AQUI ES EL ERROR

if(isset($_POST["idUsuario"])){

    $editar = new AjaxUsuarios();
    $editar -> idUsuario = $_POST["idUsuario"];
    $editar -> ajaxEditarUsuario();
}  

JS

Uncomment the dataType :

$(document).on("click", ".btnEditarUsuario", function(){

   var idUsuario = $(this).attr("idUsuario");
   console.log("idUsuario", idUsuario); //Mandando llamar el id con la consola

   //AJAX
   var datos = new FormData();
   datos.append("idUsuario", idUsuario);

  $.ajax({

       url:"../ajax/usuarios.ajax.php",
       method: "POST",
       data: datos,
       cache: false,
       contentType: false,
       processData: false,
       dataType: "json",
       success: function(respuesta){

           console.log("respuesta", respuesta);

       }

   });  })
    
answered by 21.04.2018 / 18:28
source
1

The question is that the controller has an ifrar to a function

require_once "../controllers/users.controller.php";     require_once "../ models / users.modelo.php";

class AjaxUsuarios{

/*=============================================
EDITAR USUARIO
=============================================*/ 

    public $idUsuario;

    public function ajaxEditarUsuario(){



    $item = "idusuario";
            $valor = $this->idUsuario;

            $respuesta = ControladorUsuarios::ctrMostrarUsuarios($item, $valor);


            echo json_encode($respuesta);

        }
    }


    /

    / EDITAR USUARIO AQUI ES EL ERROR

    public function ajaxEditarUsuariedita_datos(){
     if(isset($_POST["idUsuario"])){
            $editar = new AjaxUsuarios();
            $editar -> idUsuario = $_POST["idUsuario"];
            $editar -> ajaxEditarUsuario();
    }  
}
    
answered by 21.04.2018 в 18:57