Caio Tarifa Caio Tarifa - 7 months ago 731
Javascript Question

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

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?

Answer

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/

Comments