1 node.js express reactjs axios
我正在将 axios 与 REACT 一起使用,并想知道如何使用它来发出 put 请求以通过我设置的后端节点/express API 更新对象。
我需要能够通过 anid和 abody并且不确定如何这样做。
axios.put('/api/profile', id, body)
.then(response => {
console.log(response.data);
})
.catch(err => {
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
我不认为这会起作用,因为它需要参数 put(url, data, {headers})
此模式是使用您要更新和返回的实体/项目的 id 发出 axios 请求,如下所示:
axios.put(`/api/profile/${id}`, body); //using string interpolation
axios.put('/api/profile' + id, body); //using string concatenation
Run Code Online (Sandbox Code Playgroud)
然后,在您的 express/node 后端,您有一个与此请求的 URI 路径匹配的路由,并使用正文更新该配置文件。不确定你的数据库使用的是什么,但在伪代码中它看起来像这样:
axios.put(`/api/profile/${id}`, body); //using string interpolation
axios.put('/api/profile' + id, body); //using string concatenation
Run Code Online (Sandbox Code Playgroud)
正如上面的评论中提到的,您可以通过查询字符串来做到这一点,但这会很奇怪/反模式。
| 归档时间: |
|
| 查看次数: |
13646 次 |
| 最近记录: |