Javascript 使用fetch API通过formData传递表单上输入的所有值时出现问题
为什么formData在我的express+mongodb服务器上变为空?我对querySelector和addEventListener有一些问题,但现在还可以。但是,我没有找到将表单的所有值发送到服务器的方法。求求你,有人帮帮我吗Javascript 使用fetch API通过formData传递表单上输入的所有值时出现问题,javascript,forms,express,fetch-api,form-data,Javascript,Forms,Express,Fetch Api,Form Data,为什么formData在我的express+mongodb服务器上变为空?我对querySelector和addEventListener有一些问题,但现在还可以。但是,我没有找到将表单的所有值发送到服务器的方法。求求你,有人帮帮我吗 document.querySelector('#enviar- cadastro').addEventListener('click', Cadastrar); Cadastrar('http://localhost:5000/usuario/novo')
document.querySelector('#enviar-
cadastro').addEventListener('click', Cadastrar);
Cadastrar('http://localhost:5000/usuario/novo')
.then(response => console.log(response.json()))
.then(data => console.log(data))
.catch(error => console.log(error));
function Cadastrar(url) {
const formDados = new FormData(document.querySelector('#signup'))
return fetch(url, {
method: 'POST',
body: JSON.stringify(formDados),
headers: {
"Content-Type": "application/json"
}
})
};
首先,将console.log(formDados)记录在地籍中,看看您得到了什么。
这也会有帮助:伙计,这很有效!您共享的代码保存了我的项目!非常感谢。请向上箭头(“此答案很有用”),这样我可以更好地帮助其他人。欢迎使用堆栈溢出。请查看当您在此处获得问题的答案时要做什么。