向API(AXIOS)REACTJ发布表单数据

发布于 2025-02-05 14:45:39 字数 304 浏览 1 评论 0 原文

我正在尝试将表单数据发送到使用Axios的服务器。如您所见,我正在使用Formik。 到目前为止,我尝试了这些代码:

图片1

图片4

谢谢

I am trying to send form data to a server with axios. As you can see I am using Formik.
So far I ve tried these codes:

picture 1

picture 2

picture 4

picture 4

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2025-02-12 14:45:39

您是否尝试过以 axios.post 向后端发送 formular axios post requests 和连接
在您的第二张图片中,在 onsubmit 中发送数据可能会更容易。

onSubmit: {(values, { setSubmitting }) => {
  console.log(values) // Just to be sure you have them correct

  // POST request
    axios.post('http:localhost:portNUM', values) // Add headers if you have any.
    .then(response => console.log(response))
    .catch(error => console.log(error))
    .finally(() => setSubmitting(false))
  }
}

onsubmit 中使用使用也可能更容易,只是为了使生活变得更加轻松。如果您知道如何使用React Hooks,并且如果没有,我建议您学习它们。

Have you tried sent Formular with axios.post to your backend. Some info on axios post requests and connecting frontend to backend.

In your second picture, it might be easier to send the data in the onSubmit.

onSubmit: {(values, { setSubmitting }) => {
  console.log(values) // Just to be sure you have them correct

  // POST request
    axios.post('http:localhost:portNUM', values) // Add headers if you have any.
    .then(response => console.log(response))
    .catch(error => console.log(error))
    .finally(() => setSubmitting(false))
  }
}

It might also be easier to use a useEffect within the onSubmit, just to make life a bit easier. If you know how to use react hooks and if not, I would recommend learning them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文