Reactjs抓取在浏览器中带来一个405状态代码



我正在开发一个基于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());

如果问题恰好是重复的,请告诉我可能的现有解决方案。

谨致问候。

因此问题得到了解决。我不知道是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());

所以终点是指标ID,要发送的数据还包括指标中要更新的其他节点,更改的只是attributeValue节点。

如果有人遇到同样的挑战,无法理解这个答案,请联系我了解更多信息。

相关内容

最新更新