Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
Caio Tarifa Avatar asked Oct 06 '12 01:10

Caio Tarifa


People also ask

How do you solve uncaught TypeError illegal invocation?

Solution for error uncaught TypeError: Illegal invocationAdd processData: false, as key/value pair to ajax settings to avoid the error Uncaught TypeError: Illegal invocation while calling ajax on form submit.

What is uncaught TypeError illegal invocation?

Published on Jan 22, 2021 in JavaScript. Last updated on Apr 17, 2022. The error is thrown when calling a function whose this keyword isn't referring to the object where it originally did, i.e. when the "context" of the function is lost.

What is jQuery form data?

The jQuery Ajax formData is a method to provide form values like text, number, images, and files and upload on the URL sever. The jQuery Ajax formData is a function to create a new object and send multiple files using this object.


1 Answers

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/

like image 176
Blender Avatar answered Oct 14 '22 04:10

Blender