表单时出错"/>
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
发布评论