S
S
Sergey2015-03-20 17:54:55
JavaScript
Sergey, 2015-03-20 17:54:55

How to submit a form via ajax if enctype='multipart/form-data' is used?

I have a form. If you send it without Ajax, everything works fine. But when working through ajax, yii2 returns me an error:

PHP Fatal Error 'yii\base\ErrorException' with message 'Call to a member function saveAs() on a non-object

I think it's because I'm using enctype = 'multipart/form-data' .
Here is the js code:
$('#user-file').on('submit', function(e){
  e.preventDefault();
  var url = $('#user-file').attr('action');
    var data = $('#user-file').serialize();

    $.ajax({
    	type: "POST",
      	url: url,
      	data: data,
    });   
});

How can I make the form work through ajax?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-03-21
@butteff

$.ajax({
      url: url,
      type: 'POST',
      data: new FormData( this ),
      processData: false,
      contentType: false,
      success:function(src) {
         //делаем что-то
      }
    });

A
Alexander Tsymbal, 2015-03-21
@AlexanderTsymbal

jquery.form plugin - it works with multipart forms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question