G
G
GaserV2015-11-04 17:11:44
JavaScript
GaserV, 2015-11-04 17:11:44

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

3 answer(s)
I
Idel Gizatullin, 2015-11-04
@idelg

Look there, you thisdo not point to the form in the sixth line. Write a selector for the form there and everything will work.
And then send it formDatato the field in Ajax.data

var formData = new window.FormData($('form'))
$.ajax({
  url: '/user_file/',
  type: 'post',
  data: formData
})

M
Maxim, 2015-11-04
@maxloyko

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);
       }
});

G
GaserV, 2015-11-05
@GaserV

damn, laravel returns an error. "TokenMismatchException in VerifyCsrfToken.php line 53:"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question