如何解决json-server 404 on POST req to /{route}/{id}
我正在尝试在使用 json-server 模拟 API 时发送发布请求以更新现有记录。
我可以向特定 id 发送 GET 请求,向通用端点发送 POST 请求以创建新记录,但向特定 id 发送 POST 返回 404。如果我可以通过 GET 找到它,它怎么会是“未找到”?
SO/Github 上所有关于它的问题似乎都无关紧要——主要是不适用的编码拼写错误。 This one 似乎相关,但尚未解决。
这是我失败的请求:
await fetch(`http://localhost:8080/profile/1`,{
method: "POST",headers: {
"Content-Type": "application/json",},body: JSON.stringify({
// ...bunch of new data
}),})
.then((res) => res.json())
.catch((err) => console.log(err));
成功 POST 到通用端点:
const newProfile = await fetch(`http://localhost:8080/profile`,body: JSON.stringify({
// ...bunch of original data
}),})
.then((res) => res.json())
.catch((err) => console.log(err));
成功 GET 到特定端点
const profileData = await fetch(
`http://localhost:8080/profile/${match.params.userId}`
)
.then((res) => res.json())
.catch((err) => console.log(err));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。