I'm creating the product registration form for a website, and in it, all forms are submitted by ajax to an api. The problem is that in this particular form I need to send images to the server. I'm trying to use the FormData object for sending, but to no avail. Here's the javascript code I'm using:
$("#formProduto").submit((e) => {
var formProduto = document.getElementById('formProduto');
var formData = new FormData(formProduto);
$.ajax("/admin/produto/api", {
type: "POST",
data: formData,
success: (data) => {
//Sucesso
}
}
return e.preventDefault();
});
Using data: $("#formProduto").serialize()
(without the file, of course) ajax works normally, when I change the code above it just ignores the e.preventDefault();
and submits the form to the page itself.