AJAX function running TWICE with WordPress admin-ajax.php

I have a form submit event that calls a WordPress ajax function. The form submit event occurs only once (It is correct as expected). But the ajax function loaded TWICE (expected to occur once).

Here is a sample of my code.

#Jquery

$('#my_form').submit(function () {
        console.log(This log print ONCE);
        var name = $('#name').val();
        var email = $('#email').val();
        $.ajax({
            type: 'post',
            url: '/wp-admin/admin-ajax.php',
            data: {
               action: 'my_ajax_function',
               name: name,
               email: email,
            },
            dataType: json,
            success: function (data) {
                $('#response').html(data.message);
            }
        });
        return false; //  to prevent default form submit;
    });

#WordPress functions.php

?php
function my_ajax_function() {
    custom_log(This log print TWICE); 

    $name = (isset($_POST[name])) ? $_POST[name] : '';
    $email = (isset($_POST['email'])) ? $_POST['email'] : '';

    // call to a function to encrypt data
    encrypt_data($name, $email);
  
    // send response back to ajax
    $result['type'] = success;
    $result['message'] = Email sent successfully;
    json_encode($result);
    echo $result;
    die();
}

add_action('wp_ajax_nopriv_my_ajax_function', 'my_ajax_function'); 
add_action('wp_ajax_my_ajax_function', 'my_ajax_function');
?

Ajax functionality is worked. But the encrypt_data function called twice. (I have created a log function custom_log in my_ajax_function and it also get printed twice).

Nb: I have checked the Network tab in inspect mode. The my_ajax_function action called only once in network tab.

Why the ajax function loading multiple times (twice) ? Is there anything I am missing?

Topic ajax php Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.