2

我正在做一个项目并使用json-serverand axios.js,但我的方法有问题delete

我的axios.js

remove = (id) => {
  axios.delete('http://127.0.0.1:3000/people/', id)
    .then(function(response) {
      this.setState({
        filtered: response
      })
    }).catch(function(error) {
      console.log(error)
    })
}

路线http://127.0.0.1:3000/peoplejson.server..

和错误:

加载资源失败:服务器响应状态为 404(未找到)

有人可以帮助我吗?

4

4 回答 4

1

你可以请求正确uriDELETE方法。

例子:

DELETE /users/1

http://localhost:4000/users/1
于 2018-08-24T12:20:17.350 回答
1

尝试将您的 json 对象主键名称更改为 id,我想您可能会这样

   {
  "tasks": [
    {
      "task_id": 2,
      "task_name": "test2",
      "day": "may 20 2021",
      "reminder": true
    },
    {
      "task_id": 3,
      "task_name": "test3",
      "day": "june 2 2021",
      "reminder": true
    }
  ]
}

像这样改变并尝试,它对我有用

  {
  "tasks": [
    {
      "id": 2,
      "task_name": "test2",
      "day": "may 20 2021",
      "reminder": true
    },
    {
      "id": 3,
      "task_name": "test3",
      "day": "june 2 2021",
      "reminder": true
    }
  ]
}
于 2021-05-15T16:03:35.617 回答
1

这里有一些不同的建议。

1)不要使用逗号,而是尝试axios.delete('http://127.0.0.1:3000/people/' + id)这样它只会认为网址是http://127.0.0.1:3000/people/3或其他

2)通过配置对象传递id,所以axios.delete('http://127.0.0.1:3000/people/', {params: {id: id})

一旦你开始删除工作:我相信 DELETE 请求的响应是一个空对象,所以你将设置state.filtered = {}. 我假设您想要没有您刚刚删除的人的人员列表?

于 2018-04-25T18:12:07.917 回答
0

我刚刚遇到这个问题。axios.method(url/id) 语法似乎只适用于 GET 和 POST。对于删除,我使用了这个:

axios({
  method: 'DELETE',
  url: 'http://127.0.0.1:3000/people/' + id
});
于 2019-03-13T17:06:55.427 回答