ehah
ehah

Reputation: 695

Uncaught TypeError: Illegal invocation while sending data

$("#upload").on("change", function (evt) {
    var data = new FormData();
    var file = $('#upload').prop('files');
    data.append('file', file);

    var req = {
        action:'uploadFile',
        data:  data,
        type: 'POST',
        processData: false,
        contentType: false,

    };

    this.getJSON('upload.php', req, function(serverData)
    {
        console.log(serverData);
    }); 
});

I am getting this error while sending data to backend. Why? I even set processData and contentType false but still getting this error.

Upvotes: 0

Views: 551

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Your getJSON() syntax is wrong. what you are trying to do is to sent a POST request to the target so use $.ajax(). getJSON() takes a data object as the second param, but you are sending a ajax options object as the second param.

$("#upload").on("change", function (evt) {
    var data = new FormData();
    $.each(this.files, function (i, file) {
        data.append('file', file);
    })
    data.append('action', 'uploadFile');

    var req = {
        url: 'upload.php',
        data: data,
        type: 'POST',
        processData: false,
        contentType: false,
        dataType: 'json'
    };

    $.ajax(req).done(function () {
        //success handler
    });
});

Upvotes: 2

Related Questions