error when sending data from AngularJS (Angular1) to php

0

I have a strange problem when sending data to php with AngularJS, I have to send two Strings and an array with data, the Strings arrive correctly to php but the array arrives empty (aunqe appears as an array). I do a console.log () just before the request and everything is correct, however when you get to php doing a var_dump () the array appears empty. But the strangest thing of all is that in this program I make another request almost equal to php and if envargo this one works correctly.

this is the call:

$scope.modificarD = function() {
    $scope.datosM = [];
    $scope.datosM = angular.copy($scope.modal.datos);
    $scope.datosM.id = angular.copy(parseInt($scope.modal.datos.id));
    $scope.datosM.nombre = angular.copy($scope.modal.datos.nombre);
    $scope.datosM.descripcion = angular.copy($scope.modal.datos.descripcion);
    $scope.datosM.categoria = angular.copy($scope.modal.datos.categoria);
    $scope.datosM.idTienda = null;
    $scope.datosM.arrayIma = angular.copy($scope.modal.datos.arrayIma);
    $scope.datosM.stock = angular.copy($scope.modal.datos.stock);
    $scope.datosM.precio = angular.copy($scope.modal.datos.precio);
    $scope.datosM.estado = angular.copy($scope.modal.datos.estado);
    auxF = angular.copy($scope.modal.datos.fechaFinCampanha);
    fecha = auxF.getFullYear()+"-"+$scope.ceros(auxF.getMonth()+1)+"-"+$scope.ceros(auxF.getDate());
    $scope.datosM.fechaFinCampanha = fecha;

    console.log($scope.datosM);

    var peticion = {
    "url"    : "servidor/rutinas.php",
    "method" : "POST",
    "data"   : { opcion     : "producto",
                 accion     : "modif",
                 parametros : $scope.datosM 
               }
    };

    console.log(peticion);

    $http(peticion).then(function(bien){
    $scope.respuesta = angular.copy(bien.data);
        if($scope.respuesta.estado == undefined) {
            alert("Error desconocido");
        } else {
             if($scope.respuesta.estado == "ok") {              
                 $scope.datoss();
                 $scope.cerrarModal('#mymodal1');
                 $scope.muestraMens($scope.respuesta.mens); 
              } else {
                  $scope.cerrarModal('#mymodal1');
                  $scope.muestraMens($scope.respuesta.tipoError);
               }
         }
    },

    function(mal){
        $scope.muestraMens("Error en conexión");
    });
}

$scope.cerrarModal = function(referencia) {
    $(referencia).modal('close');
}

It should also be noted that to pass the data of the modal try to do a angular.copy() but I do not know why that variable does not work, so I had to put each value in the array one by one

here you can see what the console.log() put:

and the var_dump done from php:

$recibido = file_get_contents('php://input');
$cliente = json_decode($recibido);
$dato1 = cliente->opcion;
$dato2 = cliente->accion;
$arrayR = cliente->parametros;

var_dump($cliente);

And finally I show you another function that sends almost the same if it works so you can see what the difference is

$scope.nuevo = function() {
    $scope.datosP = angular.copy($scope.modal.datos);
    auxF = angular.copy($scope.modal.datos.fechaFinCampanha);
    $scope.datosP.idTienda = $scope.idsTiendas[$scope.nombreTiendas[$scope.modal.datos.idTienda]];
    if($scope.productoElegido!=""){
        $scope.datosP["id"] = $scope.idsProds[$scope.productoElegido];
    } 

    if(auxF != null) {
        fecha = auxF.getFullYear()+"-"+$scope.ceros(auxF.getMonth()+1)+"-"+$scope.ceros(auxF.getDate());
        $scope.datosP.fechaFinCampanha = fecha;
    }

    var peticion = {
    "url"    : "servidor/rutinas.php",
    "method" : "POST",
    "data"   : { opcion     : "producto",
                 accion     : "anhadir",
                 parametros : $scope.datosP 
               }
    };

    $http(peticion).then(function(bien){
    $scope.respuesta = angular.copy(bien.data);
        if($scope.respuesta.estado == undefined) {
            alert("Error desconocido");
        } else {
             if($scope.respuesta.estado == "ok") {              
                 $scope.datoss();
                 $scope.cerrarModal('#mymodal1');
                 $scope.muestraMens($scope.respuesta.mens); 
              } else {
                  $scope.cerrarModal('#mymodal1');
                  $scope.muestraMens($scope.respuesta.tipoError);
               }
         }
    },

    function(mal){
        $scope.muestraMens("Error en conexión");
    });
}
    
asked by ivanao 13.05.2018 в 02:53
source

1 answer

0

Well, after many laps I managed to send the data in a way that works:

$scope.modificarArriba = function() {

    auxF = angular.copy($scope.modal.datos.fechaFinCampanha);
    fecha = auxF.getFullYear()+"-"+$scope.ceros(auxF.getMonth()+1)+"-"+$scope.ceros(auxF.getDate());

    var peticion = {
    "url"    : "servidor/rutinas.php",
    "method" : "POST",
    "data"   : { opcion     : "producto",
                 accion     : "modif",
                 parametros : { id : parseInt($scope.modal.datos.id),
                                nombre : $scope.modal.datos.nombre,
                                descripcion : $scope.modal.datos.descripcion,
                                categoria : $scope.modal.datos.categoria,
                                fechaFinCampanha : fecha,
                                idTienda : null,
                                arrayIma : $scope.modal.datos.arrayIma,
                                stock : $scope.modal.datos.stock,
                                precio : $scope.modal.datos.precio,
                                estado : $scope.modal.datos.estado
                              }
               }
    };

    $http(peticion).then(function(bien){
    $scope.respuesta = angular.copy(bien.data);
        if($scope.respuesta.estado == undefined) {
            alert("Error desconocido");
        } else {
             if($scope.respuesta.estado == "ok") {              
                 $scope.datoss();
                 $scope.cerrarModal('#mymodal1');
                 $scope.muestraMens($scope.respuesta.mens); 
              } else {
                  $scope.cerrarModal('#mymodal1');
                  $scope.muestraMens($scope.respuesta.tipoError);
               }
         }
    },

    function(mal){
        $scope.muestraMens("Error en conexión");
    });
}

By sending the parameters creating the array within the call variable itself if it works, even when one of those parameters is another array

    
answered by 13.05.2018 / 17:05
source