I am calling a php page (using PHP7.0) from javascript (on Chrome Version 67.0.3396.99). Reading around the web, I used a code example. Passing a json string through PUT to php is supposed to deliver a $_POST variable that is a map.
{"a":"A","b":"B","c":"C"} becomes
$_POST = [ "a" => "A", "b" => "B", "c"=>"C" ]
However, in my code below, javascript passed instead
$_POST = [ "{"a":"A","b":"B","c":"C"}" : "" ]
This is bizarre. If not a map, I would have expected a String
Did I do something wrong? This is a little nerve-wracking to think the code is unreliable for production. Or perhaps there is a better approach?
javascript:
function testeroo(){
json_ = '{"a":"A","b":"B","c":"C"}'
jQuery.ajax({
type: "POST",
url: './test.php',
data: json_,
success: function (obj) {
alert(obj);
},
error: function () {
alert("ERROR testeroo");
}
});
}
test.php confirms this:
foreach ($_POST as $key => $value){
$zeroKey = $key;
//only one key, so $zeroKey is only key
}
$j = json_decode($zeroKey);
//$j is the expected map