Pass an array of objects via AJAX per POST and read the properties of objects in PHP

1

I'm having some problems solving this situation. I'm trying to pass my array of objects via AJAX to another page in PHP and read the properties that are inside each object within that array. For you to try to understand better I will post the code here ...

My JavaScript is assembling the object and returning this data (example values):

[{"horario":"14:00","id_bus_rota_parada":"1","id_bus_rota":"0","id_bus_parada":"22"},{"horario":"15:00","id_bus_rota_parada":"1","id_bus_rota":"0","id_bus_parada":"23"}];

These objects are inside the array "data []" ...

So I try to pass this array of objects to PHP already transformed into JSON.

$.ajax({
        url: "../actions/rota_inserir.php",
        type: "post",
        contentType: 'application/json',
        data: JSON.stringify(dados),
        dataType: 'json',
        success: function (data) {
            if (data.sucesso == true) {
                var n = noty({
                    text: "Rota cadastrada com sucesso!",
                    type: 'success'
                });
            }
        },
    });

Now I do not know how to get this data in PHP, I made a line of code but it did not work ...

$horarioParada = $_POST['horario'];
    
asked by anonymous 02.04.2017 / 02:07

2 answers

3

Assign a reference to the data you want to send pedido , and remove the definition of the contentType: 'application/json' header.

$.ajax({
    url: "../actions/rota_inserir.php",
    type: "post",
    data: {pedido: JSON.stringify(dados)},
    dataType: 'json',
    success: function (data) {
        if (data.sucesso == true) {
            var n = noty({
                text: "Rota cadastrada com sucesso!",
                type: 'success'
            });
        }
    },
});

In php, set the output to json , and check if the request was made, and then convert the data sent through pedido to an array, and return a status in json:

header("Content-Type: application/json");
if(isset($_POST['pedido'])){
  $dados = json_decode($_POST['pedido'], true);
  print json_encode(['sucesso'=>true]);
}

In the end, in the client, you can receive or not the return as json , this will depend on what you really want to do.

  

Examples: Ajax - _POST

    
02.04.2017 / 04:14
1

You do not need to do this conversion to json to send per post, it would basically pass the object with the data

$.post('../actions/rota_inserir.php', {horario: '14:00'}, function (retorno) {
    // trata o retorno
}); 

If you want to return an object from php, it also gives, basically it would have to do a json_encode on the object and give an echo

<?php 
$obj = new stdClass();
$obj->success = true;
echo json_encode($obj);

Then in this case you have to convert from when it arrives in the post

$.post('../actions/rota_inserir.php', {horario: '14:00'}, function (retorno) {
  try {
    retorno = $.parseJSON(retorno);
    if(retorno.success) {
      alert("Rota cadastrada com sucesso!");
    } 
  } catch (ex) {
    console.log(ex); 
  }
}); 
    
02.04.2017 / 04:10