Sorry for bad title. I dont understand why I get an Uncaught TypeError: Illegal invocation error on my AJAX request in wordpress.

This code works:

JS

function updateDB(){
    $.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: 'POST',
        data: {
            action: 'det_lilla_exta
        },
    });
}

PHP (server script)

<?php

add_action('wp_ajax_det_lilla_extra', 'det_lilla_extra');
add_action( 'wp_ajax_nopriv_det_lilla_extra', 'det_lilla_extra' );
function det_lilla_extra(){

    echo "hi";

}

But If I change my js (to later be able to send images via the fromdata object)

JS

function updateDB(){
    var formData = new FormData();
    formData.append('action', 'det-lilla-extra');
    $.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: 'POST',
        data: formData,
    });
}

Then I get the Uncaught TypeError: Illegal invocation error. I have done some googleing and found out that to prevent your data to be converted to a string you should put the processData parameter to false. Like done here:

function updateDB(){
    var formData = new FormData();
    formData.append('action', 'det-lilla-extra');
    $.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: 'POST',
        data: formData,
        processData: false
    });
}

But now I only recieve 0 from my wordpress php script which means that it can not find my action handler.

So Im not sure what to do here. Or what is really being wrong. Any ideas?

(I might have some slight syntax errors here since I have not copy pasted my code, but I do not have any syntax related errors in my original code).

Read more here: Illegal invocation error (if processData not set and can not find action handler if set) on ajax request in wordpress


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: