$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Using AJAX on frontend: Synchronous XMLHttpRequest deprecation except for admin role|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Using AJAX on frontend: Synchronous XMLHttpRequest deprecation except for admin role

matteradmin9PV0评论

Everything is happening in single.php. I have to validate if user has subscribed to the webinar and show login button accordingly. It's on the frontend.

I'm doing this on document ready:

<?php if(is_user_logged_in()): ?>
    var data = {
        webinar: <?php echo get_the_ID(); ?>,
        user: <?php echo get_current_user_id(); ?>,
        action: 'enter_webinar',
    }

    $.ajax({
        url : '/wp-admin/admin-ajax.php',
        type : "get",
        data: data,
        success: function(r) {
            $('#placeholder').html(r);
        }
    });
<?php endif; ?>

Div with ID placeholder is empty by default. This is what I have in functions.php:

add_action( 'wp_ajax_enter_webinar', 'enter_webinar' );
add_action( 'wp_ajax_nopriv_enter_webinar', 'enter_webinar' );
function enter_webinar() {
    // conditional statment happens here
    // if condition is true, send this via AJAX:
    <a href="#" class="btn btn-primary btn-block">Enter webinar</a>
<?php
    wp_die();
}

When I login as an admin user, everything works perfectly: AJAX request is sent, conditional statements work correctly, the data is retrieved and a login button is show in placeholder div. For other roles, homepage of the website is displayed inside the placeholder div as an iframe and Chrome displays this error message in console log:

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

What am I doing wrong here?

Post a comment

comment list (0)

  1. No comments so far