/Reactjs 获取在浏览器中带来 405 状态代码

/Reactjs fetching bringing a 405 status code in browser

我正在开发一个基于 DHIS2 和在线数据的 React 应用程序,其结构如下:

indicators: [
  {
   name: "something",
   attributeValues : [ {}],
   anotherNode: "anything",

  },
 {},
 {}, ...
]

我正在尝试更新整个 attributeValues 节点。我正在使用获取请求,但得到

405 method not allowed

你认为我做错了什么。这是我写的 fetch post 请求。

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

所以终点是indicatorID,data-to-send还包括指标中的其他节点要更新,变化的只是attributeValue节点。

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