请求Axios |反应过来,终极版
我想和爱可信PUT请求,我得到这个我的行动里面至今:请求Axios |反应过来,终极版
export function updateSettings(item) {
return dispatch => {
console.log(item)
return axios.put(`/locks`).then(response => {
console.log(response)
})
}
}
当我CONSOLE.LOG item
我可以看到所有的事情我已经在我的打字输入框内的对象,但后来我得到了404。我知道我有这个URI。有谁知道如何解决这个问题?
放置响应将需要一个对象发送。 put的正确axios是这样的:
export function updateSettings(item) {
return dispatch => {
console.log(item)
return axios.put(`/locks`, item).then(response => {
console.log(response)
})
}
}
这很可能是你为什么得到错误,因为PUT的对象是未定义的。
您可以在下面的链接中查看此列表,以了解如何使用axios制作正确的请求。 Axios request methods
A PUT请求需要资源的标识符(例如id)和要更新的有效负载。您似乎没有确定要更新的资源,因此。
你需要一个ID和项目这样。
export function updateSettings(id, item) {
return dispatch => {
console.log(item)
return axios.put(`/locks/${id}`, item).then(response => {
console.log(response)
})
}
}
将'$添加到'axios.put(\'/ locks/$ {id} \',item)'这样它就可以工作,只是复制粘贴,否则变量不会得到正确补充 –
不知道罗兰,谢谢。知道我只需要将id发送给我的操作,因为此时它尚未定义。 –
当然,您可以将答案作为*接受*来帮助其他可能错过它的人 – Rowland
我会给你一个upvote指出,但我仍然得到错误。 –
错误说的是什么?尝试在'then'之后添加'.catch(error => console.log(error))',但它也可能是API端的错误,尝试尽可能多地尝试记录何时和在哪里以及为什么你会收到404回复给你。 –
这是错误:'PUT http:// localhost:3001/locks 404(Not Found)'。 –