Answer the question
In order to leave comments, you need to log in
How to correctly send FormData?
Tell. What is the correct way to send an object? I have a form. There is an event, onsubmit="newrecord(event)". In the function itself, I need to create an object and put all the data there, and then send it. It seems like an empty object is being created. Or maybe I'm missing something. There are no errors.
function newrecord(e)
{
e.preventDefault();
var form = $(this)[0];
formData = new FormData(form);
console.log(formData);
}
Answer the question
In order to leave comments, you need to log in
Look there, you this
do not point to the form in the sixth line. Write a selector for the form there and everything will work.
And then send it formData
to the field in Ajax.data
var formData = new window.FormData($('form'))
$.ajax({
url: '/user_file/',
type: 'post',
data: formData
})
Try like this
var form = $(this)[0];
var formData = new FormData(form);
$.ajax({
url : 'upload.php', // your url
type : 'POST',
data : formData,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success : function(data) {
console.log(data);
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question