【问题标题】:WordPress ajax not returning what I expectedWordPress ajax 没有返回我所期望的
【发布时间】:2021-12-11 06:59:11
【问题描述】:

我将 sn-ps 插件与 Elementor 一起使用。我有一个像这样设置为 sn-p 的 ajax 函数:

add_action( 'wp_ajax_get_slug_from_id', 'get_slug_from_id' );
add_action( 'wp_ajax_nopriv_get_slug_from_id', 'get_slug_from_id' );

function get_slug_from_id() {
    return url_to_postid( $_POST['slug'] ); 
    die();
}

我在页面上嵌入了一些 jQuery 来尝试检索这样的帖子 ID:

<script>
    (function($){
        
        $(window).on('load',function(){
        
            $('.ee-calendar-skin--default .ee-calendar__day__event__name a').each(function(){
                
                let slug = $(this).attr('href').match( /([^/]*)\/[^\/]*$/ )[1];
                
                $.post(
                    wp.ajaxurl, 
                    {
                        'action': 'get_slug_from_id',
                        'slug': slug
                    }, 
                    function(response) {
                        console.log(response);
                    }
                );
                
            });
        
        });
        
    })(jQuery);
</script>

这只是返回页面的 HTML,如果我将 wp.ajaxurl 更改为 '/wp-admin/admin-ajax.php' 它只会返回 0

知道我哪里出错了吗?

【问题讨论】:

    标签: javascript php jquery ajax wordpress


    【解决方案1】:

    最后我想通了。将return 更改为echo 似乎是解决办法。

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多