Fetch() Javascript 方法 PUT 不起作用。错误:404

问题描述 投票:0回答:1

我有一个问题,我制作了 Discord 机器人,但 Put 方法不起作用。它给了我一个错误。同时,如果我制作一个不带附件的常规 json 文件,它就可以正常工作。我的错误是什么?这个问题已经困扰我三天了

此代码正在运行:

JSON:

{
  "id": 1,
  "title": "How to Use Fetch to Make GET Requests in React",
  "content": "In this blog post, we will learn how to use the Fetch API to make GET requests in React...",
  "author": "David",
  "date": "2023-01-01"
}

JavaScript:

const data = {
  title: "How to Use Fetch to Make PUT Requests in React",
  content: "In this blog post, we will learn how to use the Fetch API to make PUT requests in React..."
};

const requestOptions = {
  method: "PUT", // Specify the request method
  headers: { "Content-Type": "application/json" }, // Specify the content type
  body: JSON.stringify(data) // Send the data in JSON format
};

// Make the request
fetch("http://localhost:3000/economy", requestOptions)
  .then(response => response.json()) // Parse the response as JSON
  .then(data => console.log(data)) // Do something with the data
  .catch(error => console.error(error)); // Handle errors

我尝试制作附件以及发送更改:

fetch("http://localhost:3000/economy")
.then(economy => economy.json())
.then(test1 => {

  let gettur = test1[0].op[0].server


  let edit = (test1[0].op[0].server = "Bubalo");

  console.log(edit);


  const respond = fetch("http://localhost:3000/economy", {
    method: "PUT",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
    body: JSON.stringify(),
  })
.then (response=>console.log(response))

  console.log(gettur);
});

JSON:

{
  "economy": [
    {
      "op": [
        {
          "server": "1089497578141388920",
          "users": [
            {
              "id": "950379710284718111",
              "cash": 200,
              "bank": 300
            },
            {
              "id": "768486511531393024",
              "cash": 0,
              "bank": 2000
            }
          ]
        },
        {
          "server": "1089497578141388720",
          "users": [
            {
              "id": "950379710284718111",
              "cash": 200,
              "bank": 300
            },
            {
              "id": "768486511531393024",
              "cash": 0,
              "bank": 2000
            }
          ]
        }
      ],
      "id": "5b91"
    }
  ]
}

错误404:

Terminal

javascript discord.js fetch http-status-code-404 fetch-api
1个回答
0
投票

你好吗?

  1. 我看到您错过了正文行中的一个参数,也许在您的本地主机上运行的应用程序不允许正文未定义。
    const respond = fetch("http://localhost:3000/economy", {
        method: "PUT",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify(xxx), // <- Right here
      });
  1. 此外,如果前者不能解决这个问题。我不认为 404 错误可能是由于丢失数据而引起的(为此发出了 400 个错误请求)。您介意向我们发送您后端的“/ PUT”控制器吗?

希望您能找到解决方案:)。

© www.soinside.com 2019 - 2024. All rights reserved.