Javascript 尝试在rest api上修补阵列

Javascript 尝试在rest api上修补阵列,javascript,rest,xmlhttprequest,Javascript,Rest,Xmlhttprequest,我无法成功地将值推送到rest api上作为对象一部分的数组。我正在关注这一点,但我无法让它发挥作用 const xhr = new XMLHttpRequest(); const formData = { "op": "add", "path": "/residents", "value": this.state.person.id }; console.log(formData) xhr.open('PATCH', 'http://localhost:3008/planets/' + t

我无法成功地将值推送到rest api上作为对象一部分的数组。我正在关注这一点,但我无法让它发挥作用

const xhr = new XMLHttpRequest();

const formData = { "op": "add", "path": "/residents", "value": this.state.person.id };
console.log(formData)

xhr.open('PATCH', 'http://localhost:3008/planets/' + this.state.person.planetID);

xhr.setRequestHeader('Content-type', 'application/json');

xhr.responseType = 'json';

xhr.addEventListener('load', () => { 

    //patch the person const planet id 

});
xhr.send(formData);
正如for json补丁中所述,您需要在路径中附加一个“-”:

const formData = { "op": "add", "path": "/residents/-", "value": this.state.person.id };

或者指定一个显式索引,将值添加到数组中。

后端是否理解JSON修补程序,如果理解,返回的错误消息是什么?是的。这里是我得到的,而不是推到数组中,没有错误:{“编辑”:“2014-12-20T20:58:18.421Z”,“气候”:“温带,热带”,“地表水”:“8”,“名称”:“亚文IV”,“直径”:“10200”,“旋转周期”:“24”,“创建”:“2014-12-10T11:37:19.144Z”,“地形”:“丛林,雨林”,“重力”:“1标准”,“轨道周期”:“4818”,“人口”:“1000”,“身份证”:3,“居民”:[],“电影”:[1],“[物体]:”,”,不会推到数组。{“编辑”:“2014-12-20T20:58:18.423Z”,“气候”:“冰冻”,“地表水”:“100”,“名称”:“霍斯”,“直径”:“7200”,“旋转周期”:“23”创建“:”2014-12-10T11:39:13.934Z“,”地形“,”苔原,冰洞,山脉“,”重力“,”1.1标准“,”轨道周期“,”549“,”人口“,”未知“,”id“,”4“,”居民“,”,”电影“:”,“[2],“[物体]:”,”