0

I am trying to send a JSON string by Javascript to a PHP script. Unfortunately it is empty. I have already checked a hundred sites on Google and SO, but I cannot find a solution to the problem. Here is what I am doing:

My HTML/Javascript:

<head>

<script type="text/javascript">

    function button1_Click() {
    var url = 'http://192.168.2.105/testpost.php';

    var method = "POST";
    var postData = {
    "first": "John",
    "last": "Doe"
    }; 

    var xhr = new XMLHttpRequest();

    xhr.open( 'POST', url );
    xhr.setRequestHeader( 'Content-Type', 'application/json' );

    xhr.send( JSON.stringify(postData) );

    }

</script>
</head>

<body>
    <button id="button1" onclick="button1_Click()">Print</button>
</body>

And my PHP:

<?php
ob_start();
var_dump($_REQUEST);
file_put_contents('test.txt', ob_get_contents());
ob_end_close();
?>

And this is what I'm getting in test.txt:

array(0) {
} 

I have no idea, why this is empty. Can you please help?

Edit: Thanks to Edgar Villegas Alvarad and Quentin

In HTML I changed the following:

    xhr.setRequestHeader( 'Content-Type', 'text/plain' );

Then in PHP:

<?php
$json_data = json_decode($HTTP_RAW_POST_DATA);

ob_start();
echo $json_data->first;
file_put_contents('test.txt', ob_get_contents());
ob_end_close();
?>

And finally I get in test.txt:

John

That was what I needed.

user1131536
  • 537
  • 2
  • 6
  • 16
  • You are sending JSON, but you are trying to read from `$_POST` which only gets populated by data formats supported by HTML forms. – Quentin Jan 12 '14 at 19:13
  • possible duplicate of [receive json post with php](http://stackoverflow.com/questions/18866571/receive-json-post-with-php) – Quentin Jan 12 '14 at 19:14
  • It is a possible duplicate of many entries. But the problem is that none of those entries help you out if you get really stuck. – user1131536 Jan 12 '14 at 20:14
  • The answer on that duplicate tells you exactly what you needed to do. Claiming that you are sending plain text is wrong, URL encoding the JSON provides no benefit but forces you to manually decode it at the other end. – Quentin Jan 12 '14 at 20:47
  • There must be a reason why there are so many duplicates. If one was really helpful it would be enough, right? ;-) Anyhow thanks for your hint regarding URL encoding/decoding. I have removed it in the Edit above. Regarding plain text... it would be nice if you could say why - otherwise it is of no help for me and others. In other words: What needs to be done in order to make the above case correct? – user1131536 Jan 13 '14 at 06:44
  • You are sending JSON, not plain text. So say you are sending JSON, not plain text. Your original content-type header code was right. – Quentin Jan 13 '14 at 06:52
  • I assumed my original content-type was right. But it doesn't work. For testing I have just set it back to application/json and what I get is "Trying to get property of non-object..." as a result. Changing back to text/plain again, everything works fine. – user1131536 Jan 13 '14 at 09:47

1 Answers1

0

In this case, you have to send your params url encoded. So instead of this:

xhr.send( JSON.stringify(postData) )

do this:

xhr.send( "postData=" + encodeURIComponent(JSON.stringify(postData)) );

Then in php you read it with:

$postData = json_decode($_POST['postData']);

EDIT, Oh, I saw you're sending as application/json. In that case you are sending it correctly and you should read it like this:

$postData = json_decode($HTTP_RAW_POST_DATA);

$HTTP_RAW_POST_DATA gets the body of the request, exactly what you need. I hope it's available in your php env. If it's not, you can use file_get_contents("php://input") (info).

Hope this helps. Cheers

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
  • Hi Edgar. Thanks for your fast reply. I wasn't able to get one or the other way to work. BUT your hint really provided all I needed to make it work by combining both solutions. – user1131536 Jan 12 '14 at 20:16