Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表单在FormData javascript中没有数据_Javascript_Reactjs_Ajax_Post - Fatal编程技术网

表单在FormData javascript中没有数据

表单在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()); 我试图在我的代码中应用此机制,但由于某种原因,表单没有信息。我想

我正在尝试提交一个表单而不使用AJAX重定向。从答案中我看到了[best](新的URLSearchParams(新的FormData(form).entries()); )我发现(我认为)不使用jQuery的情况下:

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>