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
Can you help me?
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With