0

Hi I am trying to create AJAX call via POST method with JSON. I do not want to use jQuery. Here's my javascript code:

var xhttp = new XMLHttpRequest();
 xhttp.open("POST", "index.php", true);
 xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xhttp.onreadystatechange = function() {
 if (this.readyState == 4 && this.status == 200)
 {
   console.log(xhttp.responseText);
 }
}
var data = "POSTdata=" + JSON.stringify({ data1 : 9, data2 : 4, data3 : 87});
xhttp.send(data);

And here's my PHP code:

if(isset($_POST['POSTdata']))
{
  $version = json_decode($_POST['POSTdata']);
  print_r($version);
}
else
{
  echo "failure";
}

The print_r output is something like that:

stdClass Object
(
  [data1] => 9
  [data2] => 4
  [data3] => 87
)

So simply i just don't know how to "reach" that data to use it. Could anyone help me?

Petrus
  • 201
  • 1
  • 4
  • 15

0 Answers0