【问题标题】:Using Axios to send form data使用axios发送表单数据
【发布时间】:2018-12-24 04:26:52
【问题描述】:

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

下面的代码显示了在单击按钮以提交帖子时触发的事件。

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">

是序列化一个选项还是我必须手动设置每个值?

【问题讨论】:

    标签: javascript html laravel post axios


    【解决方案1】:

    在 JavaScript 中使用 FormData 类:

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

    【讨论】:

    • 给我这个错误:Uncaught TypeError: Failed to construction 'FormData': parameter 1 is not of type 'HTMLFormElement'。
    猜你喜欢
    • 2020-03-05
    • 2020-11-14
    • 2019-04-23
    • 2020-06-05
    • 2020-06-25
    • 2020-11-21
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多