How to pass data from the angle to a class in php?

1

I have the following code in Angular:

 <script>
 angular.module("fluxo", ["ngRoute"]);

.factory('factCliente', ['$http', function($http) {
    var _getData2 = function(id_empresa) {
    return $http.post("php/index.php", id_empresa);
};

return {
    getData2: _getData2
}
}])

.controller("fluxoCtrl", function ($scope, $http, factCliente) {

    var id_empresa = {id_empresa: 1};
    factCliente.getData2(id_empresa).then(function(response) {
        $scope.mostraTodasContasEntradas = response;
    }, function(error) {
        console.log("Ocorreu um erro: " + error);
    });
});
</script>

And my code in php that includes the class and calls the appropriate function that I want to call

<?php
 require_once "../con/conexao.php";
 require_once "../classes/contaEntrada.php";

 $entrada = new contaEntrada();

 $payload = json_decode(file_get_contents('php://input'));
 $id_empresa = $payload->id_empresa;

 $id_empresa = $_POST['id_empresa'];

 $entrada->mostraContasEntrada($id_empresa);
?>

I know the error is here, in that code in php, but I do not know where. Can anyone help me?

    
asked by anonymous 13.11.2015 / 22:25

1 answer

1

Angled posts are triggered as Request Payload, so on the PHP side you can get them that way

$payload = json_decode(file_get_contents('php://input'));
$id_empresa = $payload->id_empresa;

Also remove ; from line 2 of the script and in your controller set the company id variable as follows

var id_empresa = {id_empresa: 1};

full code working:

Script:

<script>
 angular.module("fluxo", ["ngRoute"])

.factory('factCliente', ['$http', function($http) {
    var _getData2 = function(id_empresa) {
    return $http.post("php/index.php", id_empresa);
};

return {
    getData2: _getData2
}
}])

.controller("fluxoCtrl", function ($scope, $http, factCliente) {

    var id_empresa = {id_empresa: 1};
    factCliente.getData2(id_empresa).then(function(response) {
        $scope.mostraTodasContasEntradas = response;
    }, function(error) {
        console.log("Ocorreu um erro: " + error);
    });
});
</script>

PHP:

<?php
 require_once "../con/conexao.php";
 require_once "../classes/contaEntrada.php";

 $entrada = new contaEntrada();

 $payload = json_decode(file_get_contents('php://input'));
 $id_empresa = $payload->id_empresa;

 $entrada->mostraContasEntrada($id_empresa);
?>
    
14.11.2015 / 01:46