E
E
Evgeny Antipov2018-04-16 19:37:12
JavaScript
Evgeny Antipov, 2018-04-16 19:37:12

[AJAX / JQUERY] Uncaught TypeError: Illegal invocation?

function ajax ($data){
let setup = {};
        // $data является либо new FormData(), либо объектом
        if ($data instanceof FormData) {
            setup = {
                processData: false,
                contentType: false
            };
        }

$.ajaxSetup({
            url: "/dummy_url",
            type: "POST",
            data: $data,
            dataType: "JSON",
            cache: false
});

return $.ajax(setup);
}

From the server, an $responsearray is returned to the variable with links to scripts that need to be loaded depending on the form parameters:
$.each($response, function (k, v) {
       $.getScript(v);
});

If the formdata was originally sent, I get an error:
Uncaught TypeError: Illegal invocation
    at i (jquery-3.3.1.min.js:2)
    at jt (jquery-3.3.1.min.js:2)
    at Function.w.param (jquery-3.3.1.min.js:2)
    at Function.ajax (jquery-3.3.1.min.js:2)
    at Ajax.self.request (app.ajax.js:40)
    at HTMLFormElement.<anonymous> (account.function.js?0.1:55)
    at HTMLDocument.dispatch (jquery-3.3.1.min.js:2)
    at HTMLDocument.y.handle (jquery-3.3.1.min.js:2)

If an object was sent, then the scripts are normally loaded into the DOM:
let $data = {id: 1, value: "dummy_value"}

What is the real problem? Obviously, the problem is in the formdata, but I don’t understand what exactly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Insayt, 2018-04-17
@Insayt

Specify additional options in $.ajaxSetup

var $data = new FormData();
$.ajaxSetup({
            url: "/dummy_url",
            type: "POST",
            data: $data,
            processData: false,
            contentType: false,
            dataType: "json",
});
$.getScript('/url');

processData - is responsible for ensuring that jQuery does not try to turn the object that you throw inside the request in accordance with Content-type: 'application/x-www-form-urlencoded'.
contentType - this will set jQuery's Content-type header. (MB you don't need)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question