Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement' 尝试在 Rea

编程入门 行业动态 更新时间:2024-10-08 19:51:11

Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement' 尝试在 React JS 和 Axios 中选择<a href=https://www.elefans.com/category/jswz/34/1771263.html style=表单时出错"/>

Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement' 尝试在 React JS 和 Axios 中选择表单时出错

我正在尝试在 React JS 应用程序中使用 Axios 发送 FormData。此表单应包含文本和多个文件输入。当我尝试使用 querySelector 选择表单时,它似乎不起作用。

import axios from 'axios';

function Verify() {

    const form = document.querySelector("formElement");

    console.log(form) //This says Null

    const submitApplication = (e) => {
        e.preventDefault();
        const formData = new FormData(form);

        axios.post('http://localhost:3001/stats', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then(() => {
            console.log("Successfully Sent!")
        });
    };

    return (
        <>

            <form id="formElement" encType="multipart/form-data" method="post">
                <input type="text" placeholder="Full Name" name="fname" />
                <input type="file" name="uploaded_file" />
                <button onClick={submitApplication} >Submit for Review</button>
            </form>

        </>
    );
}

export default Verify;

在此错误消息下方:

未捕获的类型错误:构造“FormData”失败:参数 1 不是“HTMLFormElement”类型

回答如下:

更多推荐

Failed to construct 'FormData': parameter 1 is not of type 'HTMLForm

本文发布于:2024-05-13 14:58:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   FormData   parameter   Failed   construct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!