使用php处理jQuery $Post


Process a jQuery $Post with php?

我需要用php处理这个(注意发送的数据是JSON):

$.post("calculate.php", existingJsonData,
    function(data) {
    //alert("Data Loaded: " + data);
    console.log("test data", data.values);
});

existingJsonData的格式如下:

{
    "object1": {
        "object11": {"x": "10", "y": "20", "z": "30"}, 
        "object12": {"x": "40", "y": "50", "z": "60"}, 
        "object13": {"x": "70", "y": "80", "z": "90"}
  }, 
    "object2": {
      "object21": {"x": "100", "y": "200", "z": "300"}, 
      "object22": {"x": "400", "y": "500", "z": "600"}, 
      "object23": {"x": "700", "y": "800", "z": "900"}
    }
}

php需要给"object2"中的每个x加1

这个将对象2中的每个x加1,并提醒您增加的值

<?php 
if ($_POST){ 
    ob_clean(); 
    $objects = $_POST['object2']; 
    foreach($objects as $a => $subObject) { 
        $objects[$a] = $subObject['x'] + 1; 
    }  
    die(json_encode((object) $objects)); 
} 
?> 
<script> 
var existingJsonData=  $.parseJSON('{    "object1": {        "object11": {"x": "10", "y": "20", "z": "30"},         "object12": {"x": "40", "y": "50", "z": "60"},        "object13": {"x": "70", "y": "80", "z": "90"} },  "object2": { "object21": {"x": "100", "y": "200", "z": "300"},  "object22": {"x": "400", "y": "500", "z": "600"}, "object23": {"x": "700", "y": "800", "z": "900"}}}');
$.post("<?php echo $_SERVER['PHP_SELF']; ?>", existingJsonData, 
    function(data) { 
    var data = $.parseJSON(data); 
    $.each(data, function(a,b){ 
       alert(a+' : '+b); 
    }); 
}); 
</script>
演示