我正在开发基于DHIS2和在线数据的react应用,其结构如下:
indicators: [
{
name: "something",
attributeValues : [ {}],
anotherNode: "anything",
},
{},
{}, ...
]
我正在尝试更新整个attributeValues节点。我正在使用提取请求,但获取
不允许使用405方法
您认为我做错了什么。这是我写的提取帖子请求。
let dataToSend = {
lastUpdated: currentTime,
created: currentTime,
value: newName,
attribute: {
id: indicatorID,
},
};
fetch(`https://www.namis.org/namis1/api/indicators/${id}/attributeValues`, {
body: JSON.stringify(dataToSend),
headers: {
Authorization: basicAuth,
"Content-type": "application/json",
},
method: "POST",
}).then((response) => response.json());
如果问题恰巧是重复的,请引导我到可能已经存在的解决方案。
致谢。
答案 0 :(得分:1)
因此问题已解决。我不知道它是否是DHIS2系统,但我不能仅更新指标的一个节点,这还因为POST用于创建不存在的节点。
因此,使用PUT请求的正确方法以及与此同时,不只是将新数据传递到attributeValues节点,而是更新整个指标节点,即相应的方法应该是:
let dataToSend = {
name: "something",
attributeValues : [ {
lastUpdated: currentTime,
created: currentTime,
value: newName,
attribute: {
id: indicatorID}
}],
anotherNode: "anything"}
fetch(`https://www.namis.org/namis1/api/indicators/${id}`, {
body: JSON.stringify(dataToSend),
headers: {
Authorization: basicAuth,
"Content-type": "application/json",
},
method: "PUT",
}).then((response) => response.json());
因此,端点是indicatorID,要发送的数据还包括要更新的指标中的其他节点,更改的只是属性值节点。
如果有人遇到同样的挑战并且无法理解此答案,请与我联系以获取更多信息。