Jquery and HTML FormData returns "Uncaught TypeError: Illegal invocation"

JqueryAjaxHtmlUpload

Jquery Problem Overview


I'm using this script to upload my image files: http://jsfiddle.net/eHmSr/

$('.uploader input:file').on('change', function() {
  $this = $(this);

  $('.alert').remove();
        
  $.each($this[0].files, function(key, file) {
    $('.files').append('<li>' + file.name + '</li>');

    data = new FormData();
    data.append(file.name, file);

    $.ajax({
      url: $('.uploader').attr('action'),
      type: 'POST',
      dataType: 'json',
      data: data
    });
  });
});

But when I click in upload button, the JavaScript console returns this error:

Uncaught TypeError: Illegal invocation 

jQuery Error

Can you help me?

Jquery Solutions


Solution 1 - Jquery

jQuery processes the data attribute and converts the values into strings.

Adding processData: false to your options object fixes the error, but I'm not sure if it fixes the problem.

Demo: http://jsfiddle.net/eHmSr/1/

Solution 2 - Jquery

I had the same problem

I fixed that by using two options

contentType: false
processData: false

Actually I Added these two command to my $.ajax({}) function

Solution 3 - Jquery

Adding processData: false to the $.ajax options will fix this issue.

Solution 4 - Jquery

My experience:

  var text = $('#myInputField');  
  var myObj = {title: 'Some title', content: text};  
  $.post(myUrl, myObj, callback);

The problem is that I forgot to add .val() to the end of $('#myInputField'); this action makes me waste time trying to figure out what was wrong, causing Illegal Invocation Error, since $('#myInputField') was in a different file than that system pointed out incorrect code. Hope this answer help fellows in the same mistake to avoid to loose time.

Solution 5 - Jquery

In my case, there was a mistake in the list of the parameters was not well formed. So make sure the parameters are well formed. For e.g. correct format of parameters

data: {'reporter': reporter,'partner': partner,'product': product}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCaio TarifaView Question on Stackoverflow
Solution 1 - JqueryBlenderView Answer on Stackoverflow
Solution 2 - JqueryHamidView Answer on Stackoverflow
Solution 3 - JqueryAnup ShettyView Answer on Stackoverflow
Solution 4 - JqueryNowdeenView Answer on Stackoverflow
Solution 5 - JqueryDeepak SrivastavView Answer on Stackoverflow