使用 json-server 正确获取删除调用?

2022-01-20 00:00:00 reactjs fetch javascript

我正在使用 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

相关文章