Angularjs - POST an array of objects (JSON data) to the php page

Asked

Viewed 439 times

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;
}

Is returning the error:

Undefined index: date

  • Your error is displayed on the Angularjs or PHP side?

  • @Celsomtrindade is presented in php

2 answers

0

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

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

0


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

Try to use the following:

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

See if that solves your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.