我有一个问题,我制作了 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:
你好吗?
const respond = fetch("http://localhost:3000/economy", {
method: "PUT",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(xxx), // <- Right here
});
希望您能找到解决方案:)。