试图在rest api上修补数组

时间:2017-07-18 21:17:03

标签: javascript rest xmlhttprequest

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

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);

1 个答案:

答案 0 :(得分:0)

如json补丁的RFC Doc中所述,您需要附加' - '走到路上:

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

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