Proper Fetch call for Delete using json-server?(使用 json-server 正确获取删除调用?)
问题描述
我正在使用 json-server 创建一个假服务器并从中获取内容.GET 可以正常工作,当我添加到 JSON 时 POST 也可以正常工作,但是当我删除任何内容时出现 404 错误.形成的 url 似乎是正确的,它确实指向我要删除的 JSON,但它仍然给我一个 404.有什么想法吗?
Im using json-server to create a fake server and Im fetching stuff from it. The GET works correctly, so does the POST when I add to the JSON, however I get a 404 error when i DELETE anything. The formed url seems to be correct, and it does point to the very JSON I want to delete, however it still gives me a 404. Any ideas?
deleteService = () => {
return fetch(
`http://${delete_url}/services?id=${this.props.data.id}&domain=${
this.props.data.domain
}`,
{
method: "DELETE"
}
)
.then(res => res.json());
};
我的 JSON 示例
{
"services": [
{
"id": "1",
"domain": "Domain1",
"service_name": "Get Users",
"service_version": "1.0.0",
"method": "GET",
"path": "/abc/def",
"protocol": "HTTP/1.1",
"host": "test.com",
"response": "200"
}
}
错误
删除 http://localhost:3100/services?id=1&domain=Domain1 404(不是找到)
DELETE http://localhost:3100/services?id=1&domain=Domain1 404 (Not Found)
即使 URL 正确存在于我的 JSON 中..
Even though the URL correctly exists on my JSON..
推荐答案
尝试使用 axios
await axios.delete(`http://localhost:3100/servicess/${this.props.data.id}`)
并在函数中将 id 作为参数传递
and in the function pass the id as a param
这篇关于使用 json-server 正确获取删除调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 json-server 正确获取删除调用?


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01