Pass dynamic array by AJAX

2

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 execute it, the answer is empty. Is the way you send the data okay? If so, is the way I receive the data okay?

Really, thank you very much for the help.

    
asked by Baker1562 21.02.2018 в 00:11
source

1 answer

2

You do not need parsear with JSON.stringify to send the data, you can simply send as clave : valor directly in the function Ajax in the following way.

$.ajax({
    url: "views/ajax/OITSave.php",
    method: "POST",
    data: {
    'datosCarga': valoresEntrada, //[1,2,3]
    'loteCaja': fieldLote,   //101
    'cajaLote': numeroCaja//1
    },
    dataType: "json",
    success: function(respuesta) {
        console.log("Se devolvio: ", respuesta);
    }
});

Then moving to the code PHP has only a small detail is in the function agregarEnCaja tries to return an attribute of its class so that as it is will fail since to access must first do $this to refer to the current object and then to its attribute.

public function agregarEnCaja(){
    echo $this->loteIngreso;
}
    
answered by 21.02.2018 / 02:12
source