使用Axios的POST方法只在数据库中插入ID.React JS



我正试图在React中使用Axios发出POST请求。显然没有问题,因为返回的状态是200〃;OK";但是只有ID被插入到数据库中。我使用以下片段来完成请求:

handleSubmit = async (e) => {
e.preventDefault();
axios.defaults.baseURL = "http://localhost:8000";
try {
console.log(this.state.form);
const response = await axios.post("/create", this.state.form);
console.log(response);
} catch (error) {
console.error(error);
}

};

我在Postman上测试了一下,似乎没有问题。Postman 测试

但是,当我试图从前端创建一个新的寄存器时,只会在数据库中插入ID。也许这张照片会有用。控制台中的响应

您所做的axios.post("/create", this.state.form)并不等同于Postman请求

在Postman请求中,您将params添加为url params,在Axios请求中,将其发送为body paramsTR

因此,您需要做的是发送Axios请求,如下所示:

await axios.post(`/create?firstname=${this.state.form.firstname}&lastname=${this.state.form.lastname}&salary=${this.state.form.salary}`,{});

如果没有的话,这应该对你有用——添加服务器路由器代码

最新更新