表单在FormData javascript中没有数据
我正在尝试提交一个表单而不使用AJAX重定向。从答案中我看到了[best](新的URLSearchParams(新的FormData(form).entries()); )我发现(我认为)不使用jQuery的情况下:表单在FormData javascript中没有数据,javascript,reactjs,ajax,post,Javascript,Reactjs,Ajax,Post,我正在尝试提交一个表单而不使用AJAX重定向。从答案中我看到了[best](新的URLSearchParams(新的FormData(form).entries()); )我发现(我认为)不使用jQuery的情况下: const form = document.querySelector('form'); const data = Object.fromEntries(new FormData(form).entries()); 我试图在我的代码中应用此机制,但由于某种原因,表单没有信息。我想
const form = document.querySelector('form');
const data = Object.fromEntries(new FormData(form).entries());
我试图在我的代码中应用此机制,但由于某种原因,表单
没有信息。我想我是在录音之前调用这个函数的?但我不知道如何修复它
我的代码(简化):
HTML(使用React和Reactstrap):
任何帮助都将不胜感激。来自
注意:FormData将仅使用使用name属性的输入字段
尝试命名输入元素:
<Reactstrap.Input name="newcomment" type="textarea" rows={4}></Reactstrap.Input>
postComment=()=>{
let xhttp = new XMLHttpRequest();
const form = document.querySelector('#commentform'); //this gets the form properly
const data = Object.fromEntries(new FormData(form).entries()); //this doesn't return anything
xhttp.open("POST", '/postComment/', false); //don't mind the fact that this is synchronized, shouldn't matter
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //I in tutorials like W3S that this is required
xhttp.send('newcomment='+data);
}
<Reactstrap.Input name="newcomment" type="textarea" rows={4}></Reactstrap.Input>