laravel 使用 Axios 发送表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51364942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:54:28  来源:igfitidea点击:

Using Axios to send form data

javascripthtmllaravelpostaxios

提问by Jamie Woods

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.

只是想知道是否可以从 Html 表单元素序列化数据,然后使用 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

这是显示如何选择数据的html

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

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

回答by DigitalDrifter

Use the FormDataclass in JavaScript:

FormData在 JavaScript 中使用该类:

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