2

I have a file which I want to send to the server. The file is being passed within a FormData object and not as a path URI. This is the code I am using:

let formData = new FormData();
formData.append('enctype', 'multipart/form-data');
formData.append('mode', 'fileInsert');
formData.append('conId', 'asdasd5535asf');
formData.append('user', 'user2422424');
formData.append('filesNumber', 1);
formData.append('msgType', 'fil');
formData.append('file0', file);

$.ajax({
    data: formData,
    success: function (a, s) {
        debugger;
    }
});

When the code reaches the $.ajax call it throws this error:

Uncaught TypeError: Illegal invocation

What is wrong? Note that the jQuery AJAX is being configured earlier, with post type, URL and such.

3
  • Try adding the following properties to the $.ajax call: contentType: false, processData: false Commented Dec 30, 2016 at 12:11
  • let me ask my server guy if anything is being received, it don't throw error now Commented Dec 30, 2016 at 12:12
  • It works, @RoryMcCrossan please give that as answer and explain what it does please Commented Dec 30, 2016 at 12:14

1 Answer 1

4

You need to set the following properties on your AJAX request:

contentType: false, 
processData: false

Setting contentType to false stops the content-type header being set. Similarly, setting processData to false will stop the content of the request being encoded, which is needed when sending a FormData object.

For more information on these and other $.ajax properties, see the jQuery Documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.