command given ajax json for php

Asked

Viewed 1,205 times

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

               }
           });

Php file:

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

I call the php page by ajax, but I can’t get the json values, come null, anyone have any idea?

  • I saw now that you had already posted this question a little bit. And posted again. It is not recommended to do this. Your question will be answered as soon as someone with knowledge of the subject and availability of time visualizes it. Posting the same question again will only cause it to be deleted.

  • ah I excluded that, because I had already posted, I thought I had not gone, I myself excluded that, sorry

  • Okay, in that case, disregard what I said.

5 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);

Observing:

In order for you to be able to check if PHP has received the data, you must show the return on 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 executing this change in the code you provided:

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

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

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

And the exit:

//object(stdClass)#1 (3) {
//  ["nome"]=>
//  string(5) "João"
//  ["cpf"]=>
//  string(3) "000"
//  ["teste"]=>
//  string(5) "teste"
//}
  • didn’t work out anyway

  • I’ll run a test, see what’s going on then.

  • i need to keep the ENVIO_RESPOSTAS also in case

  • Your code is working, just like the way I suggested it. Are you sure you’re checking it correctly? Take a look at the edit of the answer.

  • i changed to: type: "POST", dataType:"JSON", url: "http://localhost/Moodle/my/index.php", date: "data="+jsonsave+"&ENVIO_RESPOSTAS=true", as you suggested but it falls into ajax error, if I take the dataType it will however go null

  • which error message?

  • Object {readystate: 0, responseJSON: Undefined, status: 0, statusText: "error"}

  • "url: "localhost/Moodle/my/index.php";, "the code is the same? because it has a ";" where it shouldn’t be.

  • no, in the code of the same comma, I think it was editing the site

  • Ta returning null? In your original code you were not displayed the server response. Only the message "was". Explain it better.

  • it succeeds if I take the datatype: "json", now if I put will pro error <! DOCTYPE html> <html dir="Ltr" lang="en" xml:lan... 29'); }); })(); //]]> </script> </body> </html> ", status: 200, statusText: "OK"}

  • edit the question code, the way you’re doing it now.

  • change the code there

Show 8 more comments

0

I use it 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 page 2.php in the background where will send the input value with the id ="Inputprimeironome" attribute to a PHP POST with the name "name" and will return the echo of this php page and assign the div #receivedValue With some jokes so can make the site well dynamic

-1

In my case it worked the one used by Bruno Almeida:

var Primeironome=$("#Inputprimeironome"). val();

$.post("pagina2.php",{name:Primeironome}). done(Function(result){ $("div #receive"). html(result); });

and in php I used: $arr = json_decode($_POST['name']); echo $arr

-1

Try to use 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');

                   }
               });

Hug,

-2

I use it this way:

$postdata = file_get_contents('php://input');
$json = json_decode($postdata);

Browser other questions tagged

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