send json data from ajax to php

1

JS file:

var jsonsave = [];

var dado = new Object();
dado.nome= "João";
dado.cpf = "000";
dado.teste = "teste";
var jason = JSON.stringify(dado);
jsonsave.push(jason);


$.ajax({
               type: "POST",
               dataType: "JSON",
               url: "http://localhost/moodle/my/index.php",
               data: "data="+jsonsave+"&ENVIO_RESPOSTAS=true",
               success: function(data){
                   console.log('foi');
               },
               error: function(data)
               {
                    console.log(data);

               }
           });

File php:

$arquivo = $_POST['data'];
$value= json_decode($arquivo, TRUE);

I call the php page through ajax, but I can not get the json values, come null, does anyone have any ideas?

    
asked by anonymous 22.01.2017 / 21:17

3 answers

0

Try to send the data in pure JSON format:

 ...
 dataType:"json",
 url: "http://localhost/moodle/my/index.php",
 data: jason, 
 ...

And in PHP, to get the values:

$json = json_encode($_POST);
var_dump($json);

Note:

In order to be able to check if PHP has received the data, you must show the return in the terminal, or in some other way. Because I did a test and found that even the way you are doing, PHP received the data. So I recommend the following change to be able to check this:

 success: function(data){
     console.log(data);
 },

Response obtained by performing this change in the code you provided:

//string(62) ""{\"nome\":\"Jo\u00e3o\",\"cpf\":\"000\",\"teste\":\"teste  //\"}""

Having the data in this format in PHP does not make much sense, maybe you want to get a array / object . In this case, you are using the inverse function. The correct would be json_decode :

$json = json_decode($_POST['data']);
var_dump($json);

And the output:

//object(stdClass)#1 (3) {
//  ["nome"]=>
//  string(5) "João"
//  ["cpf"]=>
//  string(3) "000"
//  ["teste"]=>
//  string(5) "teste"
//}
    
22.01.2017 / 21:29
0

I use this a lot:

var PrimeiroNome=$("#InputPrimeiroNome").val();

$.post("pagina2.php",{nome:PrimeiroNome}).done(function(resultado){
   $("div #recebeValor").html(resultado);
});

Basically inside the javascript will open a page2.php in the background where it will send the value of the input with the id="InputPrimeiroName" assign to a POST to PHP with the name of "name" and will return the echo of this page php and assign to div #recebeValue With some games so you can make the site very dynamic

    
22.01.2017 / 22:38
0

Try using as follows:

    $.ajax({
                   type: "POST",
                   url: "http://localhost/moodle/my/index.php",
                   data: function (data){
                      data.data = jason;
                      data.envio_resposta= true;
                   },
                   success: function(data){
                       console.log('foi');
                   },
                   error: function(data)
                   {
                        console.log('n foi');

                   }
               });

Hugs,

    
22.01.2017 / 23:11