I am trying to send a simple json object by $_POST to a php from an AJAX call. But everything I do is returning an empty array. Past posts have suggested using 'file_get_contents(x)', 'if(isset(x)){}' and nothing seems to work.
print_r($_POST);
$data = file_get_contents('php://input');
echo "line before";
print_r(json_decode($data, true));
//return json_decode($data);
if (isset($_POST['x']))
{
$myfile = $_POST['x'];
print_r($myfile);
print"hello paul, here I am";
} else {
print"something is rotten in the country of Denmark";
}
I have attempted to read and access the data multiple ways. the first print_r returns an empty array, the second print_r does not even fire, and the if fails.
On the Javascript side I have tried multiple ways of sending the data both with and without JSON_stringify(). I have tried sending a string instead of JSON, as well as sending JSON data in the data stream.
$.ajax({
type: 'POST',
url: 'admo.php/',
data: x,
//datatype: "JSON",
success: function() {
console.log("Success: " + x)
window.open("admo.php");
},
error: function(x) {
console.log("Error: " + x)
console.log("you errored out....some where");
}
});
The data x could be any json file, just for an example
{"AlarmClockTimes": ["10:24", "5:13", "14:43", "17:00"],
"RadioStatus": "off",
"RadioStations": ["wamu", "bluegress", "soil", "comedy"]}
I get the window to open, and the console.log to print which tells me that is it is sending, but PHP is not receiving. Any help would be greatly appreciated.
Per a suggestion about network and HTTP response codes, they both come back good. When using the network tab on chrome developer, I see a HTTP response of 200 and see the form data is correct.