theoutstandingme
theoutstandingme

Reputation: 65

Uploading is stuck

$("#sendingForm").submit(

        function ()
        {
            if (checkForm())
                sendForm();
            return false;
        }

    );
function sendForm ()
        {
            var formData = new FormData($("#sendingForm"));
            $.ajax({
                    url: '/process.php',
                    method: 'post',
                    contentType: 'multipart/form-data',
                    data: formData,
                    success: function(response){
                        if (response!="0")
                            errorsDecode(response);
                        else
                            alrightMes();
                        alert(response);
                    }
                });
        }

I used to serialize form data, but then I had to add file uploading, so i use FormData class. but it doesn't work. I can't find out the reason. Help me please.

Upvotes: 0

Views: 45

Answers (1)

charlietfl
charlietfl

Reputation: 171669

FormData won't unwrap a jQuery object representing the form element.

Try changing:

var formData = new FormData($("#sendingForm"));

To

var formData = new FormData($("#sendingForm")[0]);

You also need to prevent processing of the FormData object internally by $.ajax and browser will set the multi-part content type when a FormData object is used

$.ajax({
  url: '/process.php',
  method: 'post',
  contentType: false,// browser will set multi-part when FormData used
  processing: false,
  data: formData,
  success: function(response) {
    if (response != "0")
      errorsDecode(response);
    else
      alrightMes();
    alert(response);
  }
});

Upvotes: 1

Related Questions