Put request with Axios | React, Redux

倾然丶 夕夏残阳落幕 提交于 2019-12-06 14:30:48

问题


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?


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/45536892/put-request-with-axios-react-redux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!