犯罪嫌疑人X
那里的噪音太多了,我无法轻松地看出问题所在。我不会尝试推理出来,而是向您展示一个解决方案,它接收数据就像您硬编码它一样,向whiteboardData数组添加一个新元素,然后再返回它。我希望使用 fetch 而不是旧的 XMLHttpRequest,但我有点生疏了。这对我来说更容易。HTML<!doctype html><html><head><script>"use strict";function byId(id){return document.getElementById(id)}window.addEventListener('load', onLoaded, false);var jsonObj = [ /* list of room objects */ { /* a room object */ name: 'example', id: 'numbers', whiteboardData: [ { /* a shape object */ color: [100, 150, 100], pointList: [ { /* a vector object*/ x: 100, y: 100, } /* heaps of other vector objects */ ] } ], /* end of whiteboardData */ chatMessages: [ { /* a message object */ sender: 'username', content: 'hi' } /* heaps of other message objects */ ] } /* heaps of other room objects */];function ajaxPostFormData(url, formData, onSuccess, onError){ var ajax = new XMLHttpRequest(); ajax.onload = function(){onSuccess(this);} ajax.onerror = function(){onError(this);} ajax.open("POST",url,true); ajax.send(formData);}function onLoaded(evt){ let fd = new FormData(); fd.append('whiteboardData', JSON.stringify(jsonObj) ); ajaxPostFormData('blahBlah.php', fd, function(ajax){console.log(ajax.responseText)}, function(){} );}</script></head><body></body></html>PHP<?php// blahBlah.php var_dump($_POST['whiteboardData']); $jsonObj = json_decode( $_POST['whiteboardData'] ); $newObj = new wbData(200,300,200, [new vec2d(0,0), new vec2d(10,10), new vec2d(100,100)] ); // append the new stuff $jsonObj[0]->whiteboardData[] = $newObj; var_dump( json_encode($jsonObj) );//----------------------------------- class wbData { public function __construct ($r,$g,$b, $pts=[]) { $this->color = [$r, $g, $b]; //$this->pointList = []; $this->pointList = $pts; } }; class vec2d { public function __construct($x=0, $y=0) { $this->x = $x; $this->y = $y; } }?>结果在控制台string(164) "[{"name":"example","id":"numbers","whiteboardData":[{"color":[100,150,100],"pointList":[{"x":100,"y":100}]}],"chatMessages":[{"sender":"username","content":"hi"}]}]"string(250) "[{"name":"example","id":"numbers","whiteboardData":[{"color":[100,150,100],"pointList":[{"x":100,"y":100}]},{"color":[200,300,200],"pointList":[{"x":0,"y":0},{"x":10,"y":10},{"x":100,"y":100}]}],"chatMessages":[{"sender":"username","content":"hi"}]}]"