angularjs - POST an array of objects (JSON data) for the php page

0
$scope.informations = [
    {
        "name": "asd",
        "rg": "1123",
        "certificado": null,
        "sex": null,
        "date": null
    },
    {
        "name": "fsdf233412423",
        "rg": "123123",
        "certificado": null,
        "sex": null,
        "date": null
    },
    {
        "name": "d23423423423423423",
        "rg": "123123123123123123",
        "certificado": null,
        "sex": null,
        "date": null
    }
];


$scope.enviar = function(){
    var url = 'gerar-algo.php';
    var informations = $scope.informations;
    $http ({
        method: 'POST',
        url: url,
        params: {
            data: informations
        }
    }).then (function (data, status, headers, config) {
        alert(JSON.stringify(data));
    });
};

PHP

$data = json_decode($_POST['data'], true);

foreach($data as $valor){
    echo $valor;
}

You are returning the error:

  

Undefined index: data

    
asked by anonymous 21.02.2017 / 20:30

2 answers

0

As you commented that your error is in PHP, it is probably by the means of receiving the data.

Try using the following:

$post = json_decode(file_get_contents("php://input"));
$data = json_decode(json_encode($post),true);

See if that solves your problem.

    
21.02.2017 / 21:12
0

Try to use $ http.post () and see the return, remember to instantiate an error callback in your promise, to make sure that the problem is not the service level, in your case, in your case PHP.

// Exemplo 
$http.post(url, {data: $scope.informations})
.then(function(response){
    console.log(response);
}, function(error){
    console.log(error);
});
    
21.02.2017 / 20:53