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:
Thank you
CodePudding user response:
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.