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 正确获取删除调用?


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- addEventListener 在 IE 11 中不起作用 2022-01-01