I have trouble in accessing the JSON string being sent from my client side Javascript code to my PHP code. This is what I have done until now.
var formatted;
fr.onload = function(e) {
var result = JSON.parse(e.target.result);
formatted = JSON.stringify(result, null, 2);
console.log(formatted);
//send formatted as post to write2share.php and write that in a file in share folder
xhr = new XMLHttpRequest();
var url = "write2share.php";
var nametime = gettime();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", 'application/json; charset=UTF-8');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var json = JSON.parse(xhr.responseText);
console.log(json.content);
}
}
var data = JSON.stringify({"content":formatted, "name": nametime});
xhr.send(data);
//GenerateShare(nametime);
}
Here formatted is a JSON string and nametime is an integer
The below is my php script
$decoded = json_decode(file_get_contents("php://input"),true);
$error = json_last_error();
var_dump($decoded);
echo $error;
The output of my $error is 0 and var_dump of $decoded is Null. Why?
Help is appreciated! Thanks