简体   繁体   English

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

[英]/Reactjs fetching bringing a 405 status code in browser

I am developing a react app over DHIS2 and data online that is structured like:我正在通过 DHIS2 和在线数据开发一个反应应用程序,其结构如下:

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

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

I am trying to update the whole attributeValues Node.我正在尝试更新整个属性值节点。 I'm using a fetch request, but getting我正在使用获取请求,但得到

405 method not allowed 405 方法不允许

What do you suppose i'm doing wrong.你认为我做错了什么。 This is the fetch post request i wrote.这是我写的获取帖子请求。

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

If the question happens to be a duplication, please direct me to the possible already existing solution.如果问题恰好是重复的,请指导我可能已经存在的解决方案。

Regards.问候。

so the issue got resolved.所以问题得到了解决。 I don't know if its the DHIS2 system or something, but I can't update just one node of an indicator, also because POST is for creating nodes that don't exist.我不知道它是 DHIS2 系统还是什么,但我不能只更新指标的一个节点,也因为 POST 用于创建不存在的节点。

So the correct way to use a PUT request, and also at the same time, instead of just passing the new data to the attributeValues node, am updating the whole indicator node ie the corrent way should be:因此,使用 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());

SO the end point is the indicatorID, and the data-to-send also includes other nodes in the indicator to be updated, what changes is the attributeValue node only.所以终点是indicatorID,要发送的数据还包括要更新的指标中的其他节点,变化的只是attributeValue节点。

If anyone meets the same challenge and is unable to understand this answer, contact me for more.如果有人遇到相同的挑战并且无法理解此答案,请与我联系以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM