使用Axios发送表单数据

8

想知道是否可以将从Html表单元素序列化的数据,然后使用Axios进行post请求发送数据。

以下是代码,展示了当点击按钮提交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);
    });
}

以下是选择数据的html代码示例:
<form method="POST" action="http://core-site.test/venue/{{$venue->slug_field}}" accept-charset="UTF-8" id="venueForm">

序列化是否可选,还是我必须手动设置每个值?

1个回答

17

使用 JavaScript 中的 FormData 类:

var form = document.querySelector('form');
var data = new FormData(form);
axios.post('/example', data);

请给我这个错误:Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'。 - Fernando Torres

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接