EDITED
I am trying to send the following data with AJAX
var datosInsert = JSON.stringify({
'datosCarga': valoresEntrada, //[1,2,3]
'loteCaja': fieldLote, //101
'cajaLote': numeroCaja //1
});
$.ajax({
url: "views/ajax/OITSave.php",
method: "POST",
data: datosInsert,
async: false,
cache: false,
contentType: false,
processData: false,
//dataType: "json",
success: function(respuesta) {
console.log("Se devolvio: ", respuesta);
}
});
I am receiving them and "assigning" them in the following way to the file where I direct the ajax
<?php
require_once "../../controllers/OIT.php";
require_once "../../models/OIT.php";
#CLASES
#**********************************************************************
class Ajax{
#RECIBE DATOS PARA INGRESO CAJA
public $loteIngreso;
public $cajaIngreso;
public $recorrido;
public $valoresCaja=[];
public function agregarEnCaja(){
echo $loteIngreso;
}
}
#OBJETOS
#************************************************************************
if(isset($_POST['loteCaja'])){
$c = new Ajax();
$c -> loteIngreso = $_POST['loteCaja'];
$c -> cajaIngreso = $_POST['cajaLote'];
$c -> valoresCaja = $_POST['datosCarga'];
$c -> agregarEnCaja();
}
?>
But when I run it the response is empty. Is the way I send the data ok? If so, is the way I receive the data okay?
Really, thank you very much for the help.
You don't need to
parsear
withJSON.stringify
to send the data, you can just send asclave : valor
directly in the functionAjax
as follows.Then, passing to the code,
PHP
it has only one small detail: in the function, itagregarEnCaja
tries to return an attribute of its class, so as it is, it will fail since to access it, it must first do so$this
to refer to the current object and then to its attribute.