Put request with Axios | React, Redux

2020-07-30 01:16发布

I want to make a put request with Axios and I got this inside of my action so far:

export function updateSettings(item) {
  return dispatch => {
    console.log(item)
    return axios.put(`/locks`).then(response => {
      console.log(response)
    })
  }
}

When I console.log item I can see all of things I've typed in my input boxes inside of that object, but later I get 404. I know that I have that URI. Does anyone know how to troubleshoot this problem?

2条回答
冷血范
2楼-- · 2020-07-30 02:08

a put response will need an object to send with. the correct axios for put is this:

export function updateSettings(item) {
    return dispatch => {
        console.log(item)
        return axios.put(`/locks`, item).then(response => {
            console.log(response)
        })
    }
}

this is most likely why you get the error because the object to PUT with is undefined.

You can watch this list on the link below, to how to make the correct requests with axios. Axios request methods

查看更多
小情绪 Triste *
3楼-- · 2020-07-30 02:12

A PUT request requires an identifier(e.g id) for the resource and the payload to update with. You seem not to be identifying the resource you want to update, hence 404.

you'd need an id and item like this.

export function updateSettings(id, item) {
  return dispatch => {
    console.log(item)
    return axios.put(`/locks/${id}`, item).then(response => {
        console.log(response)
    })
  }
}
查看更多
登录 后发表回答