5

i'm doing a project and using json-server and axios.js, but i am with problem in the method delete.

My 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)
    })
}

The route http://127.0.0.1:3000/people is of the json.server..

And the error :

Failed to load resource: the server responded with a status of 404 (Not Found)

Someone would can help me ?

7 Answers 7

4

you could request to the proper uri with DELETE method.

example:

DELETE /users/1

http://localhost:4000/users/1
Sign up to request clarification or add additional context in comments.

Comments

3

try and change your json objects primary key name as id,I think you might have like this

   {
  "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
    }
  ]
}

change like this and try, It worked for me

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

Comments

1

Here are a few different suggestions.

1) instead of using a comma, try axios.delete('http://127.0.0.1:3000/people/' + id) so it will just think the url is http://127.0.0.1:3000/people/3 or whatever

2) pass the id through a config object, so axios.delete('http://127.0.0.1:3000/people/', {params: {id: id})

Once you get the deleting working: I believe the response of a DELETE request is an empty object, so you'd be setting state.filtered = {}. I assume you want the list of people without the person you just deleted?

6 Comments

Thank you for your answer, exacty i want a list without the person deleted
in that case, you will probably need to make another call to axios.get('http://127.0.0.1:3000/people/') and the response will be the full list.
I already the method get, post too. I would want the method delete but be giving error :(
so first thing, you need to change the way you make the delete request to one of the options I provided above. Then it will not return that error. Then after that, you'll need to GET again.
could you please elaborate on last sentence? when using httpModule of angular, the DELETE method is removing whole data in db.json. GET after a DELETE return empty list.
|
1

I faced the same issue after many tries, I just restarted the JSON-server and it solve the problem.

1 Comment

Good answer. I also got problem. I tried to change everything in the code but not help. I saw your answer. And well, it works now.
0

I was just running into this issue. The axios.method(url/id) syntax seems to only work with GET and POST. For delete I used this:

axios({
  method: 'DELETE',
  url: 'http://127.0.0.1:3000/people/' + id
});

Comments

0

Try this, axios.delete(http://127.0.0.1:3000/people/${id})

Comments

0

Two things you can try:

  1. You can try sending a DELETE request to http://127.0.0.1:3000/people/${id} using Postman software or Thunder Client VS Code extension. If the response has status code 200, it means the endpoint is working fine and there is something wrong in your code somewhere.
  2. Another thing you can try is to stop the JSON server and start it again.

I was having the same problem; in my case I solved it using the 2nd option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.