使用 Axios 提交请求 | react,Redux

IT技术 reactjs redux put axios
2021-05-10 12:11:26

我想用 Axios 发出一个 put 请求,到目前为止我已经在我的操作中实现了这一点:

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

当我使用 console.log 时,item我可以看到我在该对象内的输入框中输入的所有内容,但后来我得到 404。我知道我有那个 URI。有谁知道如何解决这个问题?

2个回答

放置响应将需要一个对象来发送。put 的正确 axios 是这样的:

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

这很可能是您收到错误的原因,因为要使用 PUT 的对象未定义。

您可以在下面的链接上观看此列表,了解如何使用 axios 发出正确的请求。axios 请求方法

PUT请求需要的资源和有效载荷与更新的标识符(例如ID)。您似乎没有确定要更新的资源,因此是404

你需要一个id和这样的项目

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