Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Axios to send form data

Just wondering if it's possible to serialize data from a Html form element and then post the data using a post request with Axios.

Here is the code that shows the event that is fired when a button click occurs to submit the post.

function form_submission(e)
{
var data = document.getElementById('venueForm');

axios.post('/venue/', {


})
    .then (function (response) {
        console.log(response);
    })
    .catch(function (error) {

        console.log(error);
    });
}

Here is the html which shows how the data is selected

<form method="POST" action="http://core-site.test/venue/{{$venue->slug_field}}" accept-charset="UTF-8" id="venueForm">

Is serializing an option or do I have to set each value manually?

like image 587
Jamie Woods Avatar asked Jul 16 '18 15:07

Jamie Woods


People also ask

How do I send FormData in React Axios?

To Use Axios POST Request to Send Form Data in ReactJS First Of all, make a variable named bodyFormData with FormData(). Then You can simply append your form data in bodyFormData just like this: bodyFormData. append('userName', 'milan'); and then you can simply use this bodyFormData in your axios post request data.

How send FormData in Axios put request?

Sending a PUT Request with Axios The simplest way to make the PUT call is to simply use the put() function of the axios instance, and supply the body of that request in the form of a JavaScript object: const res = await axios. put('/api/article/123', { title: 'Making PUT Requests with Axios', status: 'published' });

How do I send a body as FormData in request?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.


1 Answers

Use the FormData class in JavaScript:

var form = document.querySelector('form');
var data = new FormData(form);
axios.post('/example', data);
like image 68
Brian Lee Avatar answered Oct 15 '22 03:10

Brian Lee